我有两个SQL选择。
首先:
SELECT v.red_club, count(v.red_club)
FROM v_round as v
GROUP BY v.red_club
它正在返回:
red_club count(v.red_club)
ABC 22
DEF 12
XYZ 4
第二次选择:
SELECT v.green_club, count(v.green_club)
FROM v_round as v
GROUP BY v.green_club
它正在返回:
green_club count(v.green_club)
ABC 5
DEF 9
XYZ 33
如何将计数加在一起(在一个选择中),以便结果如下:
club count(total)
ABC 27
DEF 21
XYZ 37
答案 0 :(得分:3)
您是否尝试过UNION-ing每个查询的结果?
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
答案 1 :(得分:0)
像这样的东西
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