我希望在分组之前看到总计数。
我有这张桌子。
+----------+------------+------------+---------+---------+--------+--------+---------+
| match_id | date | tournament | playerA | playerB | scoreA | scoreB | offline |
+----------+------------+------------+---------+---------+--------+--------+---------+
| 1 | 2012-12-04 | 799 | 4 | 55 | 1 | 3 | 0 |
| 2 | 2012-12-03 | 11921 | 2 | 41 | 2 | 0 | 0 |
| 3 | 2012-12-03 | 11921 | 21 | 41 | 0 | 2 | 0 |
| 4 | 2012-12-03 | 11921 | 3 | 2 | 2 | 1 | 0 |
| 5 | 2012-12-03 | 11921 | 41 | 2 | 1 | 2 | 0 |
| 6 | 2012-12-03 | 11921 | 21 | 3 | 1 | 2 | 0 |
| 7 | 2012-12-03 | 11924 | 1 | 8 | 2 | 1 | 1 |
| 8 | 2012-12-03 | 11924 | 1 | 8 | 2 | 3 | 1 |
| 9 | 2012-12-03 | 11924 | 8 | 19 | 3 | 2 | 1 |
| 10 | 2012-12-03 | 11924 | 19 | 12 | 2 | 1 | 1 |
+----------+------------+------------+---------+---------+--------+--------+---------+
Sqlfiddle相同:http://sqlfiddle.com/#!9/09d661/1
这就是我尝试过的。
SELECT *, count(*), count(scoreA > scoreB) as SA, count(scoreA < scoreB) as SB
from matches
Group By playerA, scoreA > scoreB, scoreA < scoreB;
然后我认为有一个子查询可能会起作用。
SELECT count(playerA)
from matches
Group By playerA
(
SELECT *, count(*), count(scoreA > scoreB) as SA, count(scoreA < scoreB) as SB
from matches
Group By playerA, scoreA > scoreB, scoreA < scoreB);
这两种方法都不适用于我。
预期结果
+----------+------------+------------+---------+---------+--------+--------+---------+----------+-----+-----+
| match_id | date | tournament | playerA | playerB | scoreA | scoreB | offline | count(*) | SA | SB |
+----------+------------+------------+---------+---------+--------+--------+---------+----------+-----+-----+
| 823 | 2012-11-04 | 3480 | 1 | 7 | 2 | 2 | 1 | 195 | 1 | 1 |
| 8 | 2012-12-03 | 11924 | 1 | 8 | 2 | 3 | 1 | 195 | 131 | 61 |
| 7 | 2012-12-03 | 11924 | 1 | 8 | 2 | 1 | 1 | 195 | 133 | 61 |
如果你看一下预期结果表中的最后几列,它是1,61和133.我真的不明白为什么SA和SB总是相同的。
1 + 61 + 133 = 195
答案 0 :(得分:0)
您的预期结果看起来不太明显,但您可能需要尝试这样的事情,
select m.playerA, count(SA.*), count(SB.*)
from matches m
inner join
(select *
from matches
where scoreA > scoreB) SA
on m.match_id = SA.match_id
inner join
(select *
from matches
where scoreA < scoreB) SB
on m.match_id = SB.match_id
group by m.playerA
答案 1 :(得分:0)
根据你发布的sqlfiddle,我认为这是你正在尝试的:
SELECT player
,team
,start_date
,end_date
,(SELECT count(player)
FROM members M2
WHERE M2.player = M.player
group by player) AS count
FROM members M
group by player, team
这是一个经过编辑的sqlfiddle: http://sqlfiddle.com/#!9/09d661/4/1