SELECT
tbl_team.te_name,
tbl_player.pl_name,
tbl_scorer.sc_time
FROM
tbl_scorer
INNER JOIN tbl_team on tbl_scorer.sID_team = tbl_team.pID_team
INNER JOIN tbl_player on tbl_scorer.sID_player = tbl_player.pID_player
INNER JOIN tbl_games on tbl_scorer.sID_game = tbl_games.pID_game
WHERE tbl_games.pID_game = 9
这给了我这样的东西
+-------+----------+---+
| TeamA | PlayerAA | 4 |
+-------+----------+---+
| TeamB | PlayerBA | 4 |
+-------+----------+---+
| TeamB | PlayerBB | 2 |
+-------+----------+---+
| TeamA | PlayerAA | 1 |
+-------+----------+---+
| TeamB | PlayerBB | 1 |
+-------+----------+---+
我想要的是通过团队列的appaerances来计算这个结果,如此
+-------+----------+---+
| TeamB | PlayerBA | 4 |
+-------+----------+---+
| TeamB | PlayerBB | 2 |
+-------+----------+---+
| TeamB | PlayerBB | 1 |
+-------+----------+---+
| TeamA | PlayerAA | 4 |
+-------+----------+---+
| TeamA | PlayerAA | 1 |
+-------+----------+---+
我可以使用
查询按团队名称分组的行数SELECT
tbl_team.te_name,
count(tbl_team.te_name)
FROM
tbl_scorer
INNER JOIN tbl_team on tbl_scorer.sID_team = tbl_team.pID_team
INNER JOIN tbl_player on tbl_scorer.sID_player = tbl_player.pID_player
INNER JOIN tbl_games on tbl_scorer.sID_game = tbl_games.pID_game
WHERE tbl_games.pID_game = 9 group by tbl_team.te_name
但如何将它们组合起来以获得正确的顺序?
答案 0 :(得分:0)
对于特定的给定数据\
select dh.* from
(SELECT
tbl_team.te_name,
tbl_player.pl_name,
tbl_scorer.sc_time
FROM
tbl_scorer
INNER JOIN tbl_team on tbl_scorer.sID_team = tbl_team.pID_team
INNER JOIN tbl_player on tbl_scorer.sID_player = tbl_player.pID_player
INNER JOIN tbl_games on tbl_scorer.sID_game = tbl_games.pID_game
WHERE tbl_games.pID_game = 9) dh,
(
SELECT
tbl_team.te_name,
count(tbl_team.te_name) rep
FROM
tbl_scorer
INNER JOIN tbl_team on tbl_scorer.sID_team = tbl_team.pID_team
INNER JOIN tbl_player on tbl_scorer.sID_player = tbl_player.pID_player
INNER JOIN tbl_games on tbl_scorer.sID_game = tbl_games.pID_game
WHERE tbl_games.pID_game = 9
) xad
where dh.te_name=xad.te_name
order by xad.rep desc,dh.sc_time desc;
答案 1 :(得分:0)
你可以用这个:
;WITH CTE AS (
SELECT
tbl_team.te_name,
tbl_player.pl_name,
tbl_scorer.sc_time
FROM
tbl_scorer
INNER JOIN tbl_team on tbl_scorer.sID_team = tbl_team.pID_team
INNER JOIN tbl_player on tbl_scorer.sID_player = tbl_player.pID_player
INNER JOIN tbl_games on tbl_scorer.sID_game = tbl_games.pID_game
WHERE tbl_games.pID_game = 9)
SELECT a.*
FROM CTE a
CROSS APPLY (SELECT COUNT(te_name) as Count_te FROM CTE b WHERE a.te_name = b.te_name) x
ORDER BY x.Count_te DESC, a.te_name, a.sc_time DESC