我正在尝试编写一个SQLite查询,该查询获取值的总出现次数除以月份。
所需输出的示例:
Name Jun Jul Aug Sept
John 3 2 5 2
Robert 4 12 45 22
Linda 0 2 1 0
我希望Name
字段仅返回唯一结果,因此每次“John”出现在Jun,Jul等月时它基本上都会计算。
这是我到目前为止使用我的实际数据库。它可以返回正确的列和唯一的“ReasonDetail”,但计数不准确:
SELECT * FROM
(
SELECT ReasonDetail AS Appetite, COUNT(*) AS June
FROM CallRecords
WHERE CallReason = 'Appetite Question'
AND DateTime BETWEEN '2017-06-01 00:00' AND '2017-07-01 00:00'
GROUP BY ReasonDetail
),
(
SELECT COUNT(*) AS July
FROM CallRecords
WHERE CallReason = 'Appetite Question'
AND DateTime BETWEEN '2017-07-01 00:00' AND '2017-08-01 00:00'
GROUP BY ReasonDetail
),
(
SELECT COUNT(*) AS August
FROM CallRecords
WHERE CallReason = 'Appetite Question'
AND DateTime BETWEEN '2017-08-01 00:00' AND '2017-09-01 00:00'
GROUP BY ReasonDetail
)
GROUP BY Appetite
我在这里做错了什么?在这种情况下,有没有更好的方法来编译正确的计数?
谢谢!
答案 0 :(得分:0)
这称为条件聚合。在聚合函数中使用 open(FILE, ">" ,$file )
FILE->autoflush;
$self->{FILEH} = *FILE;
表达式。
case