Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query problem ( Get count result )

Author  Topic 

Chengli
Starting Member

10 Posts

Posted - 2008-07-19 : 00:51:20
[code]
i got the query problem ...

How can i display the data like following:

Aid Total Count(astatus)
-------------------
A1 1
A2 0

if the sample data given is like following:

Aid ACid AStatus
-------------------
A1 1 Yes
A1 1 NO
A2 2 No
A2 2 NO


Thanks for helps....

[/code]

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-07-19 : 01:24:57
SELECT Aid,
SUM(CASE WHEN AStatus='Yes' THEN 1 ELSE 0 END) AS [Total Count]
FROM YourTable
GROUP BY Aid
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-07-19 : 01:27:32
select aid, sum(case when astatus = 'yes' then 1 else 0 end)
from table1
group by aid
order by aid


select aid, count(nullif(astatus, 'no'))
from table1
group by aid
order by aid



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -