SELECT
product_name,
CASE
WHEN product_type = '衣服' THEN
'A:' || product_type
WHEN product_type = '办公用品' THEN
'B:' || product_type
WHEN product_type = '厨房用具' THEN
'C:' || product_type
ELSE
NULL
END AS abc_product_type
FROM
Product;
ELSE
字句其实是可以不写的,默认的就是ELSE NULL
。
使用CASE表达式进行行列转换
SELECT
sum(
CASE
WHEN product_type = '衣服' THEN
sale_price
ELSE
0
END
) AS sum_price_clothes,
sum(
CASE
WHEN product_type = '厨房用具' THEN
sale_price
ELSE
0
END
) AS sum_price_kitchen,
sum(
CASE
WHEN product_type = '办公用品' THEN
sale_price
ELSE
0
END
) AS sum_price_office
FROM
product;