1

I have a select query to select an ID and corresponding count and group them based on ID and this is the sql for that

SELECT InID, COUNT(*) as ICount FROM RawData WHERE CompletedDate>= @BeginDate AND CompletedDate<= @EndDate AND InID in (3851,4151,11) GROUP BY InID 

This returns only one record for me

4151 225 

To find the missing entries ie 3851 and 11 , i tried the query

SELECT InID, COUNT(*) as ICount FROM RawData WHERE CompletedDate>= @BeginDate AND CompletedDate<= @EndDate AND InID in (3851,4151,11) GROUP BY InID HAVING COUNT(*)=0 

But it returned 0 records. So to check the IDs with missing records in a group by what is the proper way

1
  • Create a calendar table. Do an outer join. Commented Jan 10, 2018 at 9:53

4 Answers 4

1

This won't return you a row if there are no records for the specified Id's That satisfy the condition in the Where Clause. You Can try this Logic

;WITH CTE AS ( SELECT MyId = 3851 UNION ALL SELECT 4151 UNION ALL SELECT 11 ) SELECT CTE.MyId, COUNT(1) FROM CTE LEFT JOIN RawData RD ON CTE.MyId = RD.InID WHERE CompletedDate>= @BeginDate AND CompletedDate<= @EndDate GROUP BY CTE.MyId 

Or Simply this will also work for you

select InID, SUM(CASE WHEN CompletedDate>= @BeginDate AND CompletedDate<= @EndDate THEN 1 ELSE 0 END) as ICount FROM RawData whereInID in (3851,4151,11) group by InID 

To Filter records with Zero count

select InID, SUM(CASE WHEN CompletedDate>= @BeginDate AND CompletedDate<= @EndDate THEN 1 ELSE 0 END) as ICount FROM RawData whereInID in (3851,4151,11) group by InID having SUM(CASE WHEN CompletedDate>= @BeginDate AND CompletedDate<= @EndDate THEN 1 ELSE 0 END) = 0 
Sign up to request clarification or add additional context in comments.

1 Comment

Like the second approach , Can we make it to list rows with 0 count only?
1

TRY THIS: I think you can achieve your desired output through OUTER APPLY as below:

SELECT InID, rd1.ICount FROM RawData rd OUTER APPLY (SELECT COUNT(*) ICount FROM RowDate rd1 WHERE rd1.InID = rd.InID AND rd1.CompletedDate>= @BeginDate AND rd1.CompletedDate<= @EndDate) rd1 WHERE InID IN (3851,4151,11) GROUP BY InID 

IDs those doesn't have records will display NULL.

Comments

1

Using LEFT JOIN:

SELECT r.InID, ISNULL(c.ICount, 0) as ICount FROM RawData r LEFT JOIN ( select InID, COUNT(*) as ICount FROM RawData where CompletedDate>= @BeginDate AND CompletedDate<= @EndDate and InID in (3851,4151,11) group by InID ) c ON c.InID = r.InID WHERE r.InID in (3851,4151,11) 

Using OUTER APPLY:

SELECT r.InID, ISNULL(c.ICount, 0) as ICount FROM RawData r OUTER APPLY ( select COUNT(*) as ICount FROM RawData where CompletedDate>= @BeginDate AND CompletedDate<= @EndDate and InID = r.InID ) c WHERE r.InID in (3851,4151,11) 

Comments

0

You need to do a left join from a resultset that contains all the values you want to include

select ids.Id, count(InId) from (select 3851 as Id union select 4151 union select 11) ids left join RawData on ids.id = rawdata.inid where CompletedDate>= @BeginDate AND CompletedDate<= @EndDate group by Ids.id 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.