我有entry
表:
我需要做一个SELECT来接收'日期','条目数'(在那个日期),'到那个日期的条目总数'。
当我选择SELECT时:
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC
我有一个结果表
但“总计”列的数据错误。
如何正确选择?这种选择逻辑是最好的还是更有效的吗?
这是demo
答案 0 :(得分:2)
如果只是5 vs 7关闭,我认为这是因为您的选择列表中的子查询访问内联视图e1
(过滤到竞争ID = 166),本身并未过滤当还使用原始entry
表(未过滤)时。您还必须将原始表格过滤到该竞争对手。
注意下面sql中的第3行(仅更改)
SELECT e1.*,
(select count(*) from entry where date(dateCreated) <= e1.date
and competitionID=166) as Total
from (
SELECT
DATE(e.dateCreated) as "Date",
count(e.dateCreated) as "No of Entries",
sum( case when e.premium='Y' then 1 else 0 end ) as Premium,
sum( case when e.free='Y' then 1 else 0 end ) as Free,
sum( case when e.affiliateID IS NOT NULL then 1 else 0 end) as Affiliate
FROM entry e
WHERE e.competitionID=166
GROUP BY DATE(e.dateCreated)
) as e1
ORDER BY Date DESC