Anuket
Anuket

Reputation: 339

SQL join two count selects

I have two SQL selects.

First:

SELECT v.red_club, count(v.red_club) 
FROM v_round as v
GROUP BY v.red_club

and it is returning:

red_club   count(v.red_club) 
ABC        22
DEF        12
XYZ        4

Second select:

SELECT v.green_club, count(v.green_club) 
FROM v_round as v
GROUP BY v.green_club

and it is returning:

green_club   count(v.green_club) 
ABC        5
DEF        9
XYZ        33

How can I join the counts together (in one select), so that the result look like:

club   count(total) 
ABC        27
DEF        21
XYZ        37

Upvotes: 0

Views: 66

Answers (2)

brian chandley
brian chandley

Reputation: 1276

Have you tried UNION-ing the results of each query?

SELECT lbl, SUM(cnt)
FROM(
     SELECT v.red_club lbl, count(v.red_club)  cnt
     FROM v_round as v
     GROUP BY v.red_club
     UNION ALL
     SELECT v.green_club lbl, count(v.green_club) cnt
     FROM v_round as v
     GROUP BY v.green_club
)
Group by lbl

Upvotes: 3

Matt
Matt

Reputation: 3680

Something like this

    select red.red_club as club, rcount+gcount as cout 
from 
(
SELECT v.red_club, count(v.red_club) as rcount
FROM v_round as v
GROUP BY v.red_club
) as red 
inner join 
(
SELECT v.green_club, count(v.green_club) as gcount
FROM v_round as v
GROUP BY v.green_club
) as green on red.red_club = green.green_club

Upvotes: 0

Related Questions