我有以下查询:
select
DATE_FORMAT(created_at,'%d %M') as day,
count(*) as Total,
count(content_sentiment = 0) as Neutral,
count(content_sentiment < 0) as Negative,
count(content_sentiment > 0) as Positive
from Master
where content_sentiment IS NOT NULL
GROUP BY DAY(created_at)
;
这让我:
day total Neautral Negative Positive
------------------------------------------------------------
01 August 9037 9037 9037 9037
02 August 6439 6439 6439 6439
03 August 5922 5922 5922 5922
04 August 5382 5382 5382 5382
总值应该是正数,中性和负数的总和,因为不是猜测,因为每个查询都得到整个表的总计,而不是按天分组。
如何为每个积极,中性和负值包含一个分组?
答案 0 :(得分:0)
您需要条件聚合:
...
count(*) as Total,
SUM(CASE WHEN content_sentiment = 0 THEN 1 ELSE 0 END) as Neutral,
SUM(CASE WHEN content_sentiment < 0 THEN 1 ELSE 0 END) as Negative,
SUM(CASE WHEN content_sentiment > 0 THEN 1 ELSE 0 END) as Positive
...
答案 1 :(得分:0)
select
DATE_FORMAT(created_at,'%d %M') as day,
count(*) as Total,
sum(if(content_sentiment = 0,1,0)) as Neutral,
sum(if(content_sentiment < 0,1,0)) as Negative,
sum(if(content_sentiment > 0,1,0)) as Positive
from Master
where content_sentiment IS NOT NULL
GROUP BY DAY(created_at)
;