我的SQL表格采用以下格式,如何转换我的表格以将CTR作为coloum?
Date |Type |count
--------+---------------------------+------
1-Apr |Clicks |500
1-Apr |Impression |1000
1-Apr |distict user Clicks |300
1-Apr |distict user impressions |450
2-Apr |Clicks |520
2-Apr |Impression |1020
2-Apr |distict user Clicks |320
3-Apr |distict user impressions |470
我希望这会成为点击率(点击率)
例如4月1日CTR = 500/1000%= 50%
例如4月2日CTR = 520/1020%= 42.30%
Date |CTR |Distict User CTR
--------+-------+------------------
1-Apr |50% |66.60%
2-Apr |42.30% |68.08
SELECT
date ,
CalculateCTR, <-- How?
calculate DISTINCT CTR
FROM Metrics
答案 0 :(得分:2)
修改后的透视查询应该在这里工作:
SELECT
Date,
100.0*MAX(CASE WHEN Type = 'Clicks' THEN count END) /
MAX(CASE WHEN Type = 'Impressions' THEN count END) AS CTR,
100.0*MAX(CASE WHEN Type = 'distinct user Clicks' THEN count END) /
MAX(CASE WHEN Type = 'distinct user impressions' THEN count END) AS "Distinct User CTR"
FROM Metrics
GROUP BY Date;