You can do it with conditional aggregation and COALESCE()
so that you get 0
instead of NULL
:
SELECT Date,
COALESCE(AVG(CASE WHEN Terms = 'Sandwich' THEN Stars END), 0) Sandwich,
COALESCE(AVG(CASE WHEN Terms = 'Chicken' THEN Stars END), 0) Chicken,
COALESCE(AVG(CASE WHEN Terms = 'Meat' THEN Stars END), 0) Meat,
COALESCE(AVG(CASE WHEN Terms = 'Others' THEN Stars END), 0) Others
FROM __table__
GROUP BY Date;
AVG()
returns a float.
If you want the result as integers you can use:
CAST(COALESCE(AVG(CASE WHEN Terms = 'Sandwich' THEN Stars END), 0) AS INTEGER)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…