I am using DB2 V7r1 (so no listagg unfortunatly). I need to be able to have all the descriptions for the item(ItemNum) be in one string comma separated. I have a Query I found that kind of works but I can not filter the result by item number. It will work fine with the first 100 rows of data but if I try to filter out an item that is maybe 100,000 rows down the table, it takes forever.
*EDIT, I should add that this table contains 1,460,072 records, and one Item/Operation may have up to 60 Description entries, so if any one knows of a way to maybe pre-filter the results or a more efficient way to do this i would appreciate it greatly
Here is my table: PARTS
ItemNum OpSequence DscNum Description
A-123 10 2 Desc Line 1
A-123 10 4 Desc Line 2
A-123 10 6 Desc Line 3
A-123 20 2 Desc Line 1
A-123 20 4 Desc Line 2
Z-555 10 2 Desc Line 1
Z-555 10 4 Desc Line 2
Here is the result I need (Need to filter by ItemNum and OpSequence)
ItemNum OpSequence Description
A-123 10 Desc Line 1, Desc Line 2, Desc Line 3
Here is the Query I used
with x (ItemNum, OpSequence, cnt, list, empno, len) as
(select z.ItemNum, z.OpSequence,
(select count(*) from PARTS y
where y.ItemNum=z.ItemNum
group by y.ItemNum),
cast(z.Description as varchar(100)),
rrn(z), 1
from PARTS z
where z.ItemNum = 'A-123' (HERE IS WHERE I AM TRYING TO FILTER)
union all
select x.ItemNum,
x.OpSequence,
x.cnt,
strip(x.list) ||', '|| e.Description,
rrn(e),
x.len+1
from PARTS e, x
where e.ItemNum = x.ItemNum and rrn(e) > x.empno
)
select ItemNum,OpSequence, list
from x
where len=cnt