【问题标题】:Display rows that have a zero count显示计数为零的行
【发布时间】:2016-02-10 15:44:47
【问题描述】:

我正在尝试显示行,即使它们返回的计数为零。然而没有运气。 我尝试使用左连接。

select
    a.Month,
    count(b.InsuranceFromJob) [Number of Participants without Insurance]
from
    hsAdmin.ReportPeriodLkup a
left join hsAdmin.ClientReport b on
    b.ReportPeriod = a.ReportPeriodId
where
    b.insurancefromjob = 2 and
    a.reportperiodid between (@lastReportId - 11) and @lastReportId
group by
    a.Month

【问题讨论】:

  • 尝试在 count 中使用 coalescezeroifnull 命令。请提及数据库名称。
  • 您在WHERE 子句中对表别名b 有限制,因此您有效地将LEFT OUTER JOIN 变成了INNER JOIN
  • 尝试将谓词 b.insurancefromjob = 2 移动到 ON 子句
  • 谢谢@GiorgosBetsos 成功了。

标签: sql count aggregate


【解决方案1】:

因为 clientreport 在 where,所以只有 clientreport 中存在的行才会在结果集中。

将check移到join处,你会得到想要的结果:

select
    a.Month,
    count(b.InsuranceFromJob) [Number of Participants without Insurance]
from
    hsAdmin.ReportPeriodLkup a
left join hsAdmin.ClientReport b on
    b.ReportPeriod = a.ReportPeriodId
and b.insurancefromjob = 2
where
    a.reportperiodid between (@lastReportId - 11) and @lastReportId
group by
    a.Month

【讨论】:

    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多