如何在SQL Server中仅当count大于0时返回count?[duplicate]

rpppsulh  于 2022-12-10  发布在  SQL Server
关注(0)|答案(1)|浏览(206)

This question already has answers here:

SQL - HAVING vs. WHERE (9 answers)
Closed 2 days ago.
I want the count() function query return value if count is greater than 0 in SQL Server.
I am using this query, but it returns created date-wise count. If I remove created column from group then it causes an error.

select count(*) as error_count 
from error_info 
group by created 
having count(*) > 0 
  and convert(datetime, created)  >= '2022-12-01 10:01:41.000' 
  and convert(datetime, created) < '2022-12-07 08:59:08.290'

As per my requirement, this query should return total records as count between given dates only if records exist in table. it should not return 0 count.
I want total records as count between given dates only if greater than 0.

6ie5vjzr

6ie5vjzr1#

where子句中使用筛选器,而不在having

select error_count from (
    select count(*) as error_count 
    from error_info 
    where
     created  >= '2022-12-01 10:01:41.000' 
    and created < '2022-12-07 08:59:08.290') e
   where error_count>0

相关问题