简体   繁体   English

SQL计数记录,值为0

[英]SQL count records with 0 Value

I'm using a SQL query for an app, but I cant get it to count values with 0 results. 我正在为应用程序使用SQL查询,但是我无法让它对结果为0的值进行计数。

The code is shown below: 代码如下所示:

select CarrierPosition, COUNT(*) as FailCount
from tbl_0000000024,
     tbl_0000000025
where asictestdate >= DATEADD(hour,-6, getdate())
  and FK_AS0000000032 = fk_as0000000056
  and fk_as0000000046 = '135'
  and FailReason = '151'
  and tbl_0000000025.Status = ' Current'
group by CarrierPosition
Order by CarrierPosition

It should show up a fail count for 8 positions, but leaves out positions 1 and 4 as they have a FailCount of 0. How do I get it to show the other 2 rows? 它应该显示8个位置的失败计数,但由于FailCount为0,因此省略了位置1和4。如何显示其他2行呢?

Thanks 谢谢

Do a LEFT JOIN , modern explicit syntax, to get those rows too. 做一个LEFT JOIN ,现代的显式语法,也要获得这些行。

select CarrierPosition, COUNT(*) as FailCount
from tbl_0000000024
LEFT JOIN tbl_0000000025 ON FK_AS0000000032 = fk_as0000000056
where asictestdate >= DATEADD(hour,-6, getdate())
  and fk_as0000000046 = '135'
  and FailReason = '151'
  and tbl_0000000025.Status = ' Current'
group by CarrierPosition
Order by CarrierPosition

(Perhaps it should be the other way, the column and table names don't describe very well. Switch LEFT to RIGHT and see what happens if the above query doesn't work as expected.) (也许应该是另一种方式,列名和表名描述得不太好。将LEFT切换为RIGHT,看看上面的查询不能按预期工作会发生什么。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM