Hi team, i have a query where i need to swap columns data to rows. below is the example of table data and required format.
Table data:
Product size manualSales ScanningSales TotalSales
AC 0 0 0
AF 0 0 0
AP 0 0 0
BS 0 0 0
CP 0 219 219
DD 0 2 2
Output
Product_Size AC AF AP BS CP DD TotalSales
ScanningSales 0 0 0 0 219 0 219
ManualSales 0 0 0 0 0 2 2
Total 0 0 0 0 0 2 2
Please help me opn this...
Note: In table product size column data doesnot fix as above ,sizes comes static in future.
Uday DodiyaPosted Jan 12, 2023, 10:11 AM
Another Solution , Here he don't want to add total_sales data, we get that data using sql and here we also get all columns total at the end
Create Table
Insert Data
Query
Output
Uday DodiyaPosted Jan 12, 2023, 5:38 AM
Create Table
Insert Data
Query - This is dynamic query if there is more than above row the below query also give output
Output
Jignesh KumarPosted Jan 12, 2023, 4:55 AM
Hello,
Please use this link which will help you out,
https://www.c-sharpcorner.com/UploadFile/f0b2ed/pivot-and-unpovit-in-sql-server/
https://www.sqlshack.com/dynamic-pivot-tables-in-sql-server/