Select AdClick.AdMonth AS DATE,
(SELECT SUM(AdBudget) FROM AdBudget GROUP BY AdMonth) /
(SELECT SUM(AdClicks) FROM AdClick, AdReport
WHERE AdReport.AdSourceID != 4
AND AdReport.AdSourceID = AdClick.AdSourceID
GROUP BY AdMonth) AS CPC
FROM AdClick, AdBudget, AdReport
WHERE AdClick.AdYear = '2016'
GROUP BY AdClick.AdMonth
SUM(AdBudget)
和SUM(AdClicks)
在表格中附加了AdMonth
值以分隔月份,但我需要划分具有相同月份值的SUM。分组不起作用,因为我不能将子查询中的多个值除以具有多个值的另一个子查询,所以有没有办法将AdMonth 1的总和除以AdMonth 1的总和和2总和2 ...?
仅供参考:我使用代码创建ColdFusion图表,但这些查询在ColdFusion文档中调用,因此使用图表的ColdFusion解决方案不在桌面之外。
答案 0 :(得分:1)
应该是这样的。内部将表连接在月份和源ID上,并按月分组:
SELECT AdClick.AdMonth AS `DATE`
, SUM(AdBudget.AdBudget)
, SUM(AdClick.AdClicks)
, SUM(AdBudget.AdBudget) / SUM(AdClick.AdClicks)
FROM AdClick
INNER JOIN AdBudget ON AdClick.AdMonth = AdBudget.AdMonth
INNER JOIN AdReport ON AdReport.AdSourceID = AdClick.AdSourceID
WHERE AdClick.AdYear = '2016'
AND AdReport.AdSourceID != 4
GROUP BY AdClick.AdMonth