Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have below table

Level	Check	Count
0	     N	
1	     Y	     0
2	     Y	
1	     N	     0
1	     N	     0
1	     N	     5
2	     N	
2	     N	
2	     N	
2	     N	
2	     N


how to get the count of N in check column which is 5 for level 2 as shown in table and result has to populate in count column as 5

What I have tried:

I tried with count and lead but not getting correct output
Posted
Updated 18-Mar-21 1:17am
v2
Comments
Maciej Los 18-Mar-21 7:14am    
Show us what you have tried.

Probably something like:
SQL
SELECT COUNT(Level) FROM [table_name] WHERE Check='N';

See SQL COUNT(), AVG() and SUM() Functions[^]
 
Share this answer
 
Comments
Member 14936854 18-Mar-21 6:51am    
Thank for the query But i need count at column level ends with 1 as shown in table for level=2
Richard MacCutchan 18-Mar-21 7:06am    
Sorry, I do not understand what you are asking for. The COUNT() function is quite easy to use, you just need to select the appropriate column and the conditions in the WHERE clause.
Member 14936854 18-Mar-21 6:53am    
in above table if you see count of 2 in level is 5 and which is populated to last value or end of level 1
Maciej Los 18-Mar-21 7:14am    
There's nothing like "end of level 1"!
Maciej Los 18-Mar-21 7:17am    
5ed!
The problem is very similar to this one: Need help on SQL query on value check[^].

Good luck!

[EDIT]

SQL
SELECT [Level], [Check], Cnt = CASE
  WHEN [Level] = 1 AND LEAD([Level]) OVER(ORDER BY [Level]) =2 THEN
    (SELECT COUNT([Level]) FROM AAA WHERE [Level]=2 AND [Check] = 'N')
  ELSE NULL END
FROM AAA a


SQL Server 2019 | db<>fiddle[^]
 
Share this answer
 
v2
Comments
Member 14936854 18-Mar-21 10:36am    
Nope it’s different then this I have to populate count where level ends at 1
Maciej Los 18-Mar-21 10:52am    
As i said, it's quite similar. The difference is in a subquery. That's all.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900