我有一个带有字段的表
TPI
CLICKS
IMPRESSION
CLASSIFY
我需要得到3个结果
1st) SUM(CLICKS)/SUM(IMPRESSION) * 100 GROUP BY TPI
2nd) SUM(IMPRESSION) WHERE CLASSIFY = "XYZ" GROUP BY TPI
3rd) SUM(IMPRESSION) WHERE CLASSIFY = "ABC" GROUP BY TPI
所需的OP
TPI 1st_data 2nd_data 3rd_data
其中TPI在输出中是唯一的 我可以在单个查询中实现吗?
预先感谢
答案 0 :(得分:1)
条件聚合应该适合您。在CASE
的参数中使用sum()
仅在满足impression
的条件时才使用值classify
。
SELECT tpi,
sum(clicks) / sum(impression) * 100 1st_data,
sum(CASE classify
WHEN 'XYZ'
THEN impression
ELSE
0
END) 2nd_data,
sum(CASE classify
WHEN 'ABC'
THEN impression
ELSE
0
END) 3rd_data
FROM elbat
GROUP BY tpi;