0

Not sure how to describe this so I will show example:

table PAGES

id      int
parent  int
name    nvarchar
status  tinyint

table PAGES_MODULES

id          int 
id_parent   int
module_type nvarchar
module_id   int
status      int

One page can have more than one linked modules. Example records:

id    parent    name     status
1     -1        Xyz      1
2     -1        Yqw      1

id    id_parent    module_type    module_id     status
1     1            ARTICLE        1             1
2     1            GALLERY        2             1
3     2            CATEGORY       3             1

What I need is to create select which will not return 2 results if I do select left join page_modules.

I would like to have select which returns linked modules as this:

id    parent    name     status    modules
1     -1        Xyz      1         ARTICLE GALLERY
2     -1        Yqw      1         CATEGORY

Is that possible?

Thanks.

UPDATE

I have tried COALESE, CROSS APPLY and SELECT within SELECT methods and came to these conclusions:

http://blog.feronovak.com/2011/10/multiple-values-in-one-column-aka.html

Hope I can publish these here, not meaning to spam or something.

feronovak
  • 2,687
  • 6
  • 35
  • 54

4 Answers4

2

You'd need to create a custom aggregate function that could concatenate the strings together, there is no built-in SQL Server function that does this.

You can create a custom aggregate function (assuming your using the latest version of SQL) using a .Net assembly. Here's the MS reference on how to do this (the example in the article is actually for a CONCATENATE function just like you require): http://msdn.microsoft.com/en-us/library/ms182741.aspx

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
  • pretty tricky stuffy. http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-ms-sql-server-2005 – MarianP Oct 27 '11 at 17:06
1

Use group_concat() to smoosh multiple rows' worth of data into a single field like that. Note that it does have a length limit (1024 chars by default), so if you're going to have a zillion records being group_concatted, you'll only get the first few lines worth unless you raise the limit.

SELECT ..., GROUP_CONCAT(modules SEPARATOR ' ')
FROM ...
GROUP BY ...

Note that it IS an aggregate function, so you must have a group-by clause.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    i think thats MySQL only, don't think MSSQL has an equiv aggregate function. – Dylan Smith Oct 27 '11 at 17:01
  • AH. Doh, right. I did find some workarounds for it somewhere at one point. here's one on so: http://stackoverflow.com/questions/451415/simulating-group-concat-mysql-function-in-ms-sql-server-2005 – Marc B Oct 27 '11 at 17:06
1
-- ==================
-- sample data
-- ==================
declare @pages table
(
    id int,
    parent int,
    name nvarchar(max),
    status tinyint
)

declare @pages_modules table
(
    id int,
    id_parent int,
    module_type nvarchar(max),
    module_id int,
    status int
)

insert into @pages values (1, -1, 'Xyz', 1)
insert into @pages values (2, -1, 'Yqw', 1)

insert into @pages_modules values (1, 1, 'ARTICLE', 1, 1)
insert into @pages_modules values (2, 1, 'GALLERY', 2, 1)
insert into @pages_modules values (3, 2, 'CATEGORY', 3, 1)


-- ==================
-- solution
-- ==================
select 
    *,
    modules = (
        select module_type + ' ' from @pages_modules pm
        where pm.id_parent = p.id
        for xml path('')
    )
from @pages p
Alexey
  • 909
  • 6
  • 11
0

You need to join both tables and then GROUP BY by pages.id, pages.parent, pages.status, pages.name and pages.status. Your modules field in your resultset is then a string aggregate function, i.e in Oracle LISTAGG(pages_modules.modules, ' ') as modules.

Fabian Barney
  • 14,219
  • 5
  • 40
  • 60