SQL selection with Distinct
With this data
Team Name Won
X Andy 1
X Cara 1
X Cara 1
X Eric 0
X Eric 0
X Eric 0
X Eric 0
Y Bill 0
Y Dave 0
Y Dave 1
Y Dave 1
I want for each team the number of players and the number of winners
Team Players Winners
X 3 2
Y 2 1
Is it done in two queries:
select team, count(distinct Name) as Players from test group by team
select team, count(distinct Name) as Winners from test where Won=1 group by team
but I cannot make out the syntax to do this in one. TIA for your help. John
+3
source to share
3 answers
Try the following:
select team, count(distinct Name) as Players, sum(won) Winners
from (select distinct * from test) tst
group by team
You can test it here: http://rextester.com/QATLK24197
+1
source to share