Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query to get multiple row into single row

I have a table in which following information are there:

ITEM  WH  BATCH  DOC NO
CLD1  FN   B1      3
CLD1  FN   B1      3
CLD1  FN   B2      3
CLD1  FN   B2      3
CLD1  FN   B3      3
CLD1  FN   B4      3

This is the code which I have used to bring the above values:

select T0.item,t0.wh,t0.batchnum from oibt t0 where t0.DOCNO = '3' and t0.Wh = 'FN'

I need the output like this:

ITEM  WH  BATCH
CLD1  FN   B1,B2,B3,B4

I have used STUFF & For XML coding too but I am not getting the desired output.

like image 904
DeePak Avatar asked Mar 04 '23 18:03

DeePak


1 Answers

SELECT ITEM, 
       WH, 
       BATCH = STUFF((SELECT ',' + BATCH
                      FROM oibt
                      WHERE [DOC NO] = '3'
                      GROUP BY BATCH
                      FOR XML PATH ('')), 1, 1, '') 
FROM oibt
GROUP BY ITEM, WH
like image 89
B.Muthamizhselvi Avatar answered Mar 13 '23 01:03

B.Muthamizhselvi