Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Firends


I'm having a table with values


Id Name Date Status

1 A 2012-10-27 1
2 B 2012-10-27 1
3 C 2012-10-28 null
4 D 2012-10-27 null


i want to take the count of columns with status null as NotVerified and status with notnull as Verified based on date

o/p is like this

NotVerified Verified
1 2

How to write the query



Regards
Chinnu
Posted
Comments
Sandeep Mewara 28-Oct-12 9:09am    
Not clear.
i want to take the count of columns with status null as NotVerified and status with notnull as Verified based on date
Elaborate.

Try this:

SQL
SELECT 
  SUM(Verified) AS Verified
 ,SUM(NotVerified) AS NotVerified
  SELECT
     NotVerified = CASE WHEN Status IS NULL THEN 1 ELSE 0 END
   , Verified    = CASE WHEN Status IS NOT NULL THEN 1 ELSE 0 END
  FROM MyTable
)AS MyStatus
 
Share this answer
 
Hi Chinnu..

I think following code block will help you

SQL
 WITH DemoCTE AS (
 SELECT [Status],[Date],ROW_NUMBER() OVER(Partition By [Date],
 [Status] ORDER BY [Date]) RowNumber FROM YourTable
 )

SELECT [Date], ISNULL((SELECT MAX(RowNumber) FROM DemoCTE WHERE [Status] IS NOT NULL AND [Date] = D.Date),0) As Verified,ISNULL((SELECT MAX(RowNumber) FROM DemoCTE WHERE [Status] IS NULL AND [Date] = D.Date ),0) NotVerified
FROM DemoCTE D GROUP BY [Date]


Thank you
 
Share this answer
 
SQL
select date, sum(NotVarified) as NotVarified, sum(Varified) as Varified from
(
    select
        date,
        case when status is null 1 else 0 end as NotVarified,
        case when status is null 0 else 1 end as Varified
    from tbl
)as temptbl
group by date

Happy Coding!
:)
 
Share this answer
 

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