Friday, April 24, 2020

PIVOT EXAMPLE2 T-SQL

This is my sample Table, i want to pivot the category column and get the sales,stock and target as rows
enter image description here
I want the sample output in this form as shown in the below wherein the categories are in place of columns and columns in place of row
enter image description here
Solution
you can create table by using the Following code
create  table Table1 
    (Branch varchar(9), Category varchar(9), Sales INT,Stock INT,Target INT)
;

INSERT INTO Table1
    (Branch, Category, Sales, Stock,Target)
VALUES
    ( 'mumbai', 'panel', 10,4,15),
    ( 'mumbai', 'AC', 11,7,14),
    ( 'mumbai', 'Ref', 7,2,10),
    ( 'Delhi', 'panel',20,4,17),
    ( 'Delhi', 'AC', 5,2,12),
    ( 'Delhi', 'Ref', 10,12,22)
;
Step1

In order to do pivot make values in the single column like below.

T- SQL code


Step 2


Let suppose category has more values so it is difficult to mention all values. So we have to use dynamic pivot.Stay tune for pivot example3 t-sql.


No comments:

Post a Comment

Sort by Month and year in the table

  Solved: Sort by month in Power BI - Microsoft Power BI Community