Hi
I have below data. Is there any way it can be done without using Pivot. Catg can be more than 40.
Item Amount Catg
A001 100 Accounts
A002 200 Accounts
A002 400 Accounts
A003 600 Hr
A004 800 Sales
I want to display like it below
Item Amount Accounts HR Sales
A001 100 100
A002 200 600
A003 600 600
A004 800
800
Thanks
Jignesh KumarPosted Mar 7, 2025, 1:47 AM
Hello Ramco,
You can use Case When with Stuff(), Please change your table name and column name as per your requirement
Muhammad Imran AnsariPosted Mar 6, 2025, 4:09 PM
Hello Ramco,
If
Catgcontains more than 40 dynamic values, you’ll need Dynamic SQL:Good Luck!
Ramco RamcoPosted Mar 6, 2025, 11:52 AM
Hi Sophia
If there are more than 40 Catg then i have to write 40 Case Statements
Thanks
Sophia CarterPosted Mar 6, 2025, 11:47 AM
Absolutely! To achieve the desired display without using Pivot, you can employ SQL's conditional aggregation. This method utilizes CASE statements to categorize data into separate columns based on conditions.
Here's how you can approach this:
In this SQL query:
- We use CASE statements within the aggregation functions to sort the data into separate columns based on the 'Catg' categories.
- The MAX function consolidates the Amount values according to the respective categories.
- Finally, we GROUP BY Item and Amount to display the data as you specified.
By executing this SQL query with your dataset, you should achieve the desired outcome displayed in a tabular format without relying on the Pivot function.