SELECT*FROM harvest_amount PIVOT(SUM(amount) AS amt FOR fruit IN ('Grape_red'AS grape_red, 'Grape_white'AS grape_white, 'Apple_red'AS apple_red););
SELECT h.date,SUM(CASEWHEN h.fruit ='Grape_red'THEN h.amount ELSE0END) AS grape_red_amt,SUM(CASEWHEN h.fruit ='Grape_white'THEN h.amount ELSE0END) AS grape_white_amt,SUM(CASEWHEN h.fruit ='Apple_red'THEN h.amount ELSE0END) AS apple_red_amt,FROM harvest_amount hGROUPBY h.date;
Pivoted table
DATE
GRAPE_RED_AMT
GRAPE_WHITE_AMT
APPLE_RED_AMT
2022-09-01
5
6
4
2022-09-03
6
4
2022-09-04
9
2
2022-09-04
5
8
5
Multiple columns
SELECT*FROM harvest_amount PIVOT(sum(amount) as amt, max(amount) as max_amt FOR fruit IN ('Grape_red', 'Grape_white', 'Apple_red'););
Unpivot
HARVEST AMOUNT
DATE
GRAPE_RED
GRAPE_WHITE
APPLE_RED
2022-09-01
5
6
4
2022-09-03
6
4
2022-09-04
9
2
2022-09-04
5
8
5
SELECT*FROM harvest_amountUNPIVOT(amount FOR fruit IN (grape_red AS'Grape (red)', grape_white AS'Grape (red)' apple_red AS'Grape (red)'););
Unpivoted table
DATE
FRUIT
AMOUNT
2022-09-01
Grape (red)
5
2022-09-01
Grape (white)
6
2022-09-01
Apple (red)
4
2022-09-03
Grape (red)
6
2022-09-03
Grape (white)
4
2022-09-04
Grape (white)
9
2022-09-04
Apple (red)
2
2022-09-05
Grape (red)
5
2022-09-05
Grape (white)
8
2022-09-05
Apple (red)
5
SELECT*FROM harvest_amountUNPIVOT INCLUDE NULLS(amount FOR fruit IN (grape_red AS'Grape (red)', grape_white AS'Grape (red)' apple_red AS'Grape (red)'););
Unpivoted table including NULLS
DATE
FRUIT
AMOUNT
2022-09-01
Grape (red)
5
2022-09-01
Grape (white)
6
2022-09-01
Apple (red)
4
2022-09-03
Grape (red)
6
2022-09-03
Grape (white)
4
2022-09-03
Apple (red)
2022-09-04
Grape (red)
2022-09-04
Grape (white)
9
2022-09-04
Apple (red)
2
2022-09-05
Grape (red)
5
2022-09-05
Grape (white)
8
2022-09-05
Apple (red)
5
Multiple columns
SELECT*FROM harvest_amt_qltUNPIVOT((amount, quality) FOR fruit IN ((grape_red_amt, grape_red_qlt) AS'Grape (red)', (grape_white_amt, grape_white_qlt) AS'Grape (red)' (apple_red_amt, apple_red_qlt) AS'Grape (red)'););