Im new to WebFocus and need a way to get multiple rows with same ID into a column separated by commas grouped by the ID.
--Before
DepId | ProId | ProCode
1 | 1 | PM
1 | 2 | IM
1 | 3 | SSO
1 | 4 | ECM
2 | 1 | PM
2 | 2 | IM
2 | 3 | SSO
3 | 2 | IM
3 | 3 | SSO
3 | 4 | ECM
--After
DepId | ProductList
1 | PM, IM, SSO, ECM
2 | PM, IM, SSO
3 | IM, SSO, ECM
Here is the SQL code example:
create table #testTable (
DepId int,
ProId int,
ProCode varchar(512)
)
insert into #testTable
select 1, 1, 'PM' union all
select 1, 2, 'IM' union all
select 1, 3, 'SSO' union all
select 1, 4, 'ECM' union all
select 2, 1, 'PM' union all
select 2, 2, 'IM' union all
select 2, 3, 'SSO' union all
select 3, 2, 'IM' union all
select 3, 3, 'SSO' union all
select 3, 4, 'ECM'
--Output before merging rows into grouped by coulumn
select * from #testTable
--Need WebFOCUS code for this output
Select
t1.DepId
,STUFF((Select ', ' + t.ProCode
From #testTable t
where t.DepId = t1.DepId
for XML Path('')
),1,2,'') ProductList
From
#testTable t1
Group by t1.DepId
drop table #testTable
--Before
DepId | ProId | ProCode
1 | 1 | PM
1 | 2 | IM
1 | 3 | SSO
1 | 4 | ECM
2 | 1 | PM
2 | 2 | IM
2 | 3 | SSO
3 | 2 | IM
3 | 3 | SSO
3 | 4 | ECM
--After
DepId | ProductList
1 | PM, IM, SSO, ECM
2 | PM, IM, SSO
3 | IM, SSO, ECM
Here is the SQL code example:
create table #testTable (
DepId int,
ProId int,
ProCode varchar(512)
)
insert into #testTable
select 1, 1, 'PM' union all
select 1, 2, 'IM' union all
select 1, 3, 'SSO' union all
select 1, 4, 'ECM' union all
select 2, 1, 'PM' union all
select 2, 2, 'IM' union all
select 2, 3, 'SSO' union all
select 3, 2, 'IM' union all
select 3, 3, 'SSO' union all
select 3, 4, 'ECM'
--Output before merging rows into grouped by coulumn
select * from #testTable
--Need WebFOCUS code for this output
Select
t1.DepId
,STUFF((Select ', ' + t.ProCode
From #testTable t
where t.DepId = t1.DepId
for XML Path('')
),1,2,'') ProductList
From
#testTable t1
Group by t1.DepId
drop table #testTable