【问题标题】:order by number of appearances in one column and query additional columns按一列中的出现次数排序并查询其他列
【发布时间】:2014-12-03 08:49:44
【问题描述】:
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 |
    +-------+----------+---+

我想要的是按照这样的团队列的出现次数来排序这个结果

+-------+----------+---+
| 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

但是如何组合它们以获得正确的顺序?

【问题讨论】:

    标签: sql sql-server count group-by sql-order-by


    【解决方案1】:

    你可以用这个:

    ;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
    

    【讨论】:

      【解决方案2】:

      对于特定的给定数据\

       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;
      

      【讨论】:

      • 这将仅按字母顺序排列第一列,而不是按此查询给定条目的行数
      • 好的@Markus我没有看到结果应该是根据出现次数...现在您可以检查结果...因为我编辑了我的答案
      猜你喜欢
      • 2014-04-24
      • 1970-01-01
      • 2011-07-01
      • 1970-01-01
      • 2018-11-09
      • 2020-11-24
      • 1970-01-01
      • 2013-09-13
      • 2018-05-28
      相关资源
      最近更新 更多