I have written a query in such a way that, I will get multiple values in single row.
This is the result I am getting now:
G.R.N Number Item No. Location Cartons BATCH REF NO
122 CBD0002 a1 184 07-04; 07-05; 07-06; 07-08; 07-09
122 CBD0002 a2 16 07-04; 07-05; 07-06; 07-08; 07-09
125 CBD0001 a1 500 200018
But I actually need an output like this:
G.R.N Number Item No. Location Cartons BATCH REF NO
122 CBD0002 a1 184 07-04; 07-05; 07-06; 07-08; 07-09
122 CBD0002 a2 16 07-09
125 CBD0001 a1 500 200018
If you compare the both tables, In the location column, The location is different for the same item, Son in the Batch Field it should display only one values.
But in my current query, it's displaying all the information which is misleading & not correct.
Have included my coding. For Reference please find the table structure from which I am picking up the information.
OIBT.BaseNum OIBT.Item No. OIBT.SuppSerial OIBT.BatchNum OBTQ.Quantity
125 CBD0001 a1 200018 500
122 CBD0002 A1 07-01 0
122 CBD0002 A1 07-02 0
122 CBD0002 A1 07-03 0
122 CBD0002 a1 07-04 48
122 CBD0002 a1 07-05 48
122 CBD0002 a1 07-06 48
122 CBD0002 a1 07-07 0
122 CBD0002 a1 07-08 40
122 CBD0002 a2 07-09 16
SELECT T0.BaseNum AS 'G.R.N Number',
T0.IntrSerial as 'G.R.N Type',
t0.ItemCode,
T0.ItemName AS 'Item Name',
t0.SuppSerial as 'Location',
sum(t1.Quantity) as 'Cartons',
count( t0.BatchNum) as 'Pallets',
[BATCH REF NO] = STUFF((SELECT DISTINCT '; ' + US.BatchNum
FROM OIBT US WHERE US.Basenum = T0.BaseNum AND US.WhsCode = T0.WhsCode AND US.ItemCode = t0.ItemCode and us.Quantity <> 0 FOR XML PATH('')), 1, 1, '')
FROM OIBT T0 INNER JOIN OBTQ T1 ON T0.ItemCode = T1.ItemCode AND T0.WhsCode = T1.WhsCode AND T0.SysNumber = T1.SysNumber
WHERE T1.WhsCode = 'SPARE' and t0.Quantity <> 0
group by T0.BaseNum,T0.itemName,t0.IntrSerial,t0.ItemCode,t0.SuppSerial,t0.WhsCode