【问题标题】:Sqlite Query to retrieve distinct values with 0 in null fieldsSqlite 查询以检索空字段中具有 0 的不同值
【发布时间】:2020-08-16 06:45:30
【问题描述】:

尝试编写查询以检索具有不同月份的数据

预期结果

months12    sev1    sev2
01  1   21
02  1   12
03  0   4
09  1   0

得到结果

months12    sev1    sev2
01  1   21
01  1   12
01  1   4
02  1   21
02  1   12
02  1   4
03  1   21
03  1   12
03  1   4
09  1   21
09  1   12
09  1   4

这里是查询

select DISTINCT months12, sev1, sev2 from
(select Distinct strftime('%m', date(issue_reported)) months12 from tickets),
(select DISTINCT strftime('%m', date(issue_reported)) months, coalesce(count(*), 0) sev1
from tickets where ticket_severity="Sev 1" and strftime('%m', date(issue_reported)) in (select DISTINCT strftime('%m', date(issue_reported)) months from tickets) Group By ticket_severity, strftime('%m', date(issue_reported))),
(select DISTINCT strftime('%m', date(issue_reported)) months1, coalesce(count(*), 0) sev2
from tickets where ticket_severity="Sev 3" and strftime('%m', date(issue_reported)) in (select DISTINCT strftime('%m', date(issue_reported)) months from tickets) Group By ticket_severity, strftime('%m', date(issue_reported)))

【问题讨论】:

  • 发布产生这些结果的数据并阐明您想要什么。

标签: sql sqlite


【解决方案1】:

我认为你需要条件聚合:

select strftime('%m', date(issue_reported)) months12, 
  sum(ticket_severity = 'Sev 1') sev1,
  sum(ticket_severity = 'Sev 2') sev2
from tickets
group by months12

【讨论】:

    猜你喜欢
    • 2012-08-08
    • 1970-01-01
    • 2017-08-26
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-11
    相关资源
    最近更新 更多