instead of using pivot/ cross tab we can also do the below which is can run on any platform SELECT months, sum(case when category='Clothing' then Amount end) as Clothing, sum(case when category='Electronic' then Amount end) as Electronic from Months group by months;
My solution with simple case statement: select month, sum(case when category = 'Clothing' then amount else 0 end) as Clothing, sum(case when category = 'Electronics' then amount else 0 end) as Electronics from sales_data_ec group by month
select month,clothing,electronics from ( select month,category,amount from sales_data )as temp pivot ( max(amount) for category in (clothing,electronics) )piv order by month
Select month, [electronics], [clothing] from ( select month, category,amount from input_tbl) as source PIVOT(sum(amount) for category in [(electronics),(clothing)] as pivot_table
select month, sum(case when category = 'Clothing' then Amount end) as Clothing, sum(case when category = 'Electronics' then Amount end) as Electronics from sales_data group by month order by month;
select month, SUM(case when category = 'Clothing' then amount end) as "Clothing", SUM(case when category = 'Electronics' then amount end) as "Electronics" from sales group by month order by month;
WITH cte_table AS( SELECT month, category, amount, CASE WHEN month = 'January' THEN 1 WHEN month = 'February' THEN 2 WHEN month = 'March' THEN 3 WHEN month = 'April' THEN 4 END AS month_order FROM sales_data ) SELECT month,Clothing,Electronics FROM ( SELECT * FROM cte_table) AS source_table PIVOT( MAX(AMOUNT) FOR CATEGORY IN(Clothing,Electronics) ) AS pivot_table ORDER BY month_order
in sql server, -- using the pivot operator select month, [Clothing], [Electronics] from ( select month, category, amount from sales_data ) as Source_Data PIVOT ( SUM(amount) FOR category IN ([Clothing], [Electronics]) ) as Pivot_Data; -- without using the pivot operator select month, SUM(case when category = 'Clothing' THEN amount else null end) as [Clothing], SUM(case when category = 'Electronics' THEN amount else null end) as [Electronis] from sales_data group by month;
SELECT month, MAX(CASE WHEN category = 'Clothing' THEN amount END) AS Clothing, MAX(CASE WHEN category = 'Electronics' THEN amount END) AS Electronics FROM sales_data GROUP BY month;
instead of using pivot/ cross tab we can also do the below which is can run on any platform
SELECT months,
sum(case when category='Clothing' then Amount end) as Clothing,
sum(case when category='Electronic' then Amount end) as Electronic
from Months
group by months;
Yes, we can use case statements as well!
My solution with simple case statement:
select
month,
sum(case when category = 'Clothing' then amount else 0 end) as Clothing,
sum(case when category = 'Electronics' then amount else 0 end) as Electronics
from
sales_data_ec
group by month
Great! Keep practicing :)
select month,clothing,electronics
from (
select month,category,amount
from sales_data
)as temp
pivot
(
max(amount)
for category in (clothing,electronics)
)piv
order by month
Select month, [electronics], [clothing] from ( select month, category,amount from input_tbl) as source
PIVOT(sum(amount) for category in [(electronics),(clothing)] as pivot_table
Nice. Keep practicing :)
select month,
sum(case when category = 'Clothing' then Amount end) as Clothing,
sum(case when category = 'Electronics' then Amount end) as Electronics
from sales_data
group by month
order by month;
Nice. Keep rocking :)
select
month,
SUM(case when category = 'Clothing' then amount end) as "Clothing",
SUM(case when category = 'Electronics' then amount end) as "Electronics"
from sales
group by month
order by month;
Great! Keep practicing :)
Would be great if you could provide table creation vommand in description
I have added the queries to create the table and insert the values. Happy Learning :)
why you are not providing create & insert statement after every video in the description box. I have given this a feedback.
I have added the queries to create the table and insert the values. Will make a note of it to add in every video. Happy Learning :)
WITH cte_table AS(
SELECT month,
category,
amount,
CASE
WHEN month = 'January' THEN 1
WHEN month = 'February' THEN 2
WHEN month = 'March' THEN 3
WHEN month = 'April' THEN 4
END AS month_order
FROM sales_data
)
SELECT month,Clothing,Electronics
FROM (
SELECT *
FROM cte_table) AS source_table
PIVOT(
MAX(AMOUNT)
FOR CATEGORY IN(Clothing,Electronics)
) AS pivot_table
ORDER BY month_order
Nice! Keep practicing :)
SELECT month,
SUM(CASE WHEN category='Clothing' then amount end) as Clothing,
SUM(CASE WHEN category='Electronics' then amount end) as Electronics
FROM sales_data
GROUP BY month;
Great! Keep practicing well! Our best wishes to you :)
in sql server,
-- using the pivot operator
select month, [Clothing], [Electronics]
from
(
select month, category, amount from sales_data
) as Source_Data
PIVOT
(
SUM(amount) FOR category IN ([Clothing], [Electronics])
) as Pivot_Data;
-- without using the pivot operator
select
month,
SUM(case when category = 'Clothing' THEN amount else null end) as [Clothing],
SUM(case when category = 'Electronics' THEN amount else null end) as [Electronis]
from sales_data
group by
month;
You're rocking :)
SELECT
month,
MAX(CASE WHEN category = 'Clothing' THEN amount END) AS Clothing,
MAX(CASE WHEN category = 'Electronics' THEN amount END) AS Electronics
FROM sales_data
GROUP BY month;
Great :)