Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table contains some values. the table is given below.

HTML
SlNo  TeamName  WorkInfo
----  -------   -------
 1       Team1        0
 2       Team1        0
 3       Team1        1
 4       Team2        1
 5       Team2        1
 6       Team3        0
 7       Team3        1


I'm using the query

>
SQL
SELECT DISTINCT TeamName, COUNT(TeamName) AS Count FROM TableA GROUP BY TeamName


The above query shows the out put like given below:

HTML
TeamName   Count
--------   -----  
 Team1        3    
 Team2        2     
 Team3        2     

But I want display the team name and count in gridview like given below based on the workInfo column values. If the `WorkInfo` Column contains zero then I have to get that count and displayed on Count2 else on Count1

HTML
TeamName   Count1   Count0
--------   -----    ------
 Team1        1        2
 Team2        2        0
 Team3        1        1


Help me to find a proper solution.Thank you.
Posted
Comments
Herman<T>.Instance 5-Jan-15 5:35am    
make a PIVOT!

SQL
select TeamName,
        count(distinct case when WorkInfo = 1 then SlNo end) as Count1   ,
        count(distinct case when WorkInfo = 0 then SlNo end) as Count0
from t1
group by TeamName

DEMO[^]
 
Share this answer
 
Comments
Member 11042100 5-Jan-15 5:56am    
@DamithSL : Thank you. It is working perfectly
Use pivot:
SQL
SELECT  TeamName, [1] AS count1, [0] AS count0
FROM
(SELECT TeamName, WorkInfo FROM tableA) AS sourcetable
PIVOT
(
  COUNT(WorkInfo) FOR WorkInfo IN ([1], [0])
)AS somename

Read more: Simple Way To Use Pivot In SQL Query[^]
 
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