简体   繁体   中英

How to bring back rows where no result set is found

  1. Here is my problem I am trying to get some counts on ID's that did not match.
  2. My expected result set is the replicationTime column (eg 201701, 201702...), when I do my count I only have NULL values for 201702, but not 201701...is there a way to bring back all the existing replicationTime values (ie 201701 AND 201702) but padded the count column with a zero or a NULL? I basically need to bring back two rows, but only one comes back which is the one that had no matches
select replicationTime, count(*) as CountsForNoMatches
from table 
where ID IS NULL
Group By ReplicationTime
  1. I have tried using ISNULL... but It is not bringing back anything other than the same row.
  2. Also, keep in mind this is the portion of a Subquery and it serves a larger picture in the overall query where I need all rows and not just one otherwise my result set returns nothing.

Like this

select replicationTime, sum(case when ID is null then 1 else 0 end) as CountsForNoMatches
from table 
Group By ReplicationTime

or

select replicationTime, max(case when ID is null then 1 else 0 end) as CountsForNoMatches
from table 
Group By ReplicationTime

if you just want to know if there is some row with a null ID for that ReplicationTime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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