I need merge rows into one row for summary of calculation (SQL Server 2014).
My SQL query is:
x
SELECT
[c].[Iso],
SUM([r].[Quantity]) AS [Quantity],
SUM([r].[Amount]) AS [Amount]
FROM
[CarReport].[dbo].[Refuelling] [r]
INNER JOIN
[CarReport].[dbo].[Currency] [c] ON [r].[CurrencyId] = [c].[CurrencyId]
WHERE
[r].[DataId] = 15
AND [r].[IsDeleted] = 0
GROUP BY
[r].[CurrencyId], [c].[Iso]
Result of this query is:
CZK | 50.00 | 1350,00
EUR | 23.00 | 463,20
I would like to have this result:
CZK/EUR | 50.00/23.00 | 1350,00/463,20
When add new currency (x), the result of new currency must be appended:
CZK/EUR/x | 50.00/23.00/x | 1350,00/463,20/x
Can somebody please help me with this topic?
Many thanks
Advertisement
Answer
Something like this should work with earlier versions of MSSQL as well.
;with subQuery as (
-- paste your base query here
-- extend it with one additional column:
-- ,ROW_NUMBER() OVER (ORDER BY ISO) RowNum
)
select
(select stuff((select '/' + convert(nvarchar(max), Iso) from subQuery order by RowNum for xml path('')), 1, 1, '')),
(select stuff((select '/' + convert(nvarchar(max), Quantity) from subQuery order by RowNum for xml path('')), 1, 1, '')),
(select stuff((select '/' + convert(nvarchar(max), Amount) from subQuery order by RowNum for xml path('')), 1, 1, ''))