Use GROUP_CONCAT to concatenate strings together, in a GROUP BY:
SELECT TransactionID, GROUP_CONCAT(ItemCode) AS ItemCodes,
SUM(Qty) AS TotalofQty, SUM(Total) AS TotalPrice, TransactionDate
FROM TableA
GROUP BY TransactionID, TransactionDate;
SqlFiddle here
Edit After changing the RDBMS to SqlServer, a hack is required to compensate for SqlServer's lack of string folding functions like GROUP_CONCAT
. Here's the STUFF / FOR XML PATH
one:
SELECT a.[Transaction ID],
STUFF((
SELECT ',' + [Item Code]
FROM TableA
WHERE [Transaction ID] = a.[Transaction ID]
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
AS ItemCodes,
SUM(Qty) AS TotalofQty, SUM(Total) AS TotalPrice, [Transaction Date]
FROM TableA a
GROUP BY a.[Transaction ID], a.[Transaction Date];
Note that you'll need to manually correlate the STUFF subquery
with the respective outer query.
SqlServer Fiddle