我使用以下语句进行了查询:
select mood, count(*) * 100/ (select count(*) from entry)from entry group by mood having data>data-30 order by mood asc
情绪是0到2之间的整数
输出是:
mood count
0 96,55
1 3,44
有没有办法添加情绪2计数0的行?
答案 0 :(得分:1)
SELECT MOOD, SUM (COUNTER) TOTAL
FROM ( SELECT 0 MOOD, 0 COUNTER FROM DUAL
UNION ALL
SELECT 1 MOOD, 0 COUNTER FROM DUAL
UNION ALL
SELECT 2 MOOD, 0 COUNTER FROM DUAL
UNION ALL
SELECT MOOD, COUNT ( * )
* 100.0
/ (SELECT COUNT ( * )
FROM ENTRY
WHERE DATA > DATE ('now') - 30)
FROM (SELECT *
FROM ENTRY
WHERE DATA > DATE ('now') - 30)
GROUP BY MOOD, DATA)
GROUP BY MOOD
ORDER BY MOOD ASC;
你必须枚举(0,1,2,.....)所有可能的数字,关联一个计数器= 0。 然后,按心情对计数器进行分组。
请注意,您拥有数据> data-30的条件是荒谬的。 您必须从ENTRY中选择满足条件数据的所有记录>日期('现在') - 例如30。
SQLite:可以按如下方式创建名为“dual”且与Oracle“双”表相同的VIEW:“CREATE VIEW dual AS SELECT'x'AS dummy;”