Forum Discussion
BADDULA
May 26, 2025Copper Contributor
Sql Query
Hi All, I have a requirement. want output data like below screen print CREATE TABLE [dbo].[Source_Table]( [Product] [varchar](10) NULL, [Trans_type] [varchar](10) NULL, [Sa...
ManiD
May 27, 2025Copper Contributor
Are you sure you want the first row's profit amount to be 16, isn't it supposed to be 20.
You could use the following query:
Select
product,
STRING_AGG(case when Trans_type = 'Type1' then SalesCode end, ',') as sales_type1,
STRING_AGG(case when Trans_type = 'Type2' then SalesCode end, ',') as sales_type2,
sum(Profitamount) Profitamount
from
[dbo].[Source_Table]
group by
product