ROW_NUMBER()ORDER BY,不能在同一SELECT语句中使用列

时间:2016-06-17 06:27:06

标签: sql sql-server

我有一个程序调用视图来创建一个联赛表:

CREATE PROCEDURE [dbo].[League_Table_Insert]
    @LeagueName VARCHAR(30)
AS
SET NOCOUNT ON
BEGIN

    DECLARE @LeagueID INT

    SELECT 
    @LeagueID = LeagueID FROM dbo.League
    WHERE LeagueName = @LeagueName

    SELECT [TeamName], [Played], [Wins], [Loss], [Draws], [Points], [Goals_Scored], [Goals_Against], [Goal_Difference]  
    FROM League_Table
    WHERE LeagueID = @LeagueID
    ORDER BY Points DESC, Goal_Difference DESC;

END

查看:

CREATE VIEW League_Table
    AS
    SELECT f.LeagueID, t.TeamName, 
    SUM(
        CASE WHEN f.HomeScore IS NOT NULL THEN 1 ELSE 0 END
    ) AS Played,
        SUM(
        CASE        
            WHEN t.TeamID = f.HomeTeamID THEN
                --Home Fixture
                CASE
                 WHEN f.HomeScore > f.AwayScore THEN 1 
                 ELSE 0
                 END
            WHEN t.TeamID = f.AwayTeamID THEN
                CASE
                 WHEN f.AwayScore > f.HomeScore THEN 1
                 ELSE 0 
                 END
        END
        ) AS Wins, 
            SUM(
        CASE        
            WHEN t.TeamID = f.HomeTeamID THEN
                --Home Fixture
                CASE
                 WHEN f.HomeScore < f.AwayScore THEN 1 
                 ELSE 0
                 END
            WHEN t.TeamID = f.AwayTeamID THEN
                CASE
                 WHEN f.AwayScore < f.HomeScore THEN 1 
                 ELSE 0
                 END
        END
        ) AS Loss, 
        SUM(CASE WHEN f.HomeScore = f.AwayScore THEN 1 ELSE 0 END) as Draws, 
        SUM(
        CASE 
            WHEN t.TeamID = f.AwayTeamID THEN
                --Away Fixture
                CASE
                    WHEN f.AwayScore > f.HomeScore THEN 3
                    WHEN f.AwayScore = f.HomeScore THEN 1
                    ELSE 0
                END
            WHEN t.TeamID = f.HomeTeamID THEN
                --Home Fixture
                CASE
                    WHEN f.HomeScore > f.AwayScore THEN 3 
                    WHEN f.HomeScore = f.AwayScore THEN 1
                    ELSE 0
                END
        END
    ) AS Points, 
        SUM(
        CASE
            WHEN t.TeamID = f.HomeTeamID THEN f.HomeScore
            -- Home Fixture
            WHEN t.TeamID = f.AwayTeamID THEN f.AwayScore
            -- Away Fixture
            END
            ) AS Goals_Scored,

        SUM(
        CASE
            WHEN t.TeamID = f.HomeTeamID THEN f.AwayScore
            -- Home Fixture
            WHEN t.TeamID = f.AwayTeamID THEN HomeScore
            -- Away Fixture
            END
            ) AS Goals_Against,
         SUM(
          CASE
          WHEN t.TeamID = f.HomeTeamID THEN       
              CASE
              WHEN f.HomeScore IS NOT NULL THEN f.HomeScore - f.AwayScore
          -- Home Fixture
              END
          WHEN t.TeamID = f.AwayTeamID THEN 
              CASE
              WHEN f.AwayScore IS NOT NULL THEN f.AwayScore - f.HomeScore
          -- Away Fixture
              END
          END
        ) AS Goal_Difference
    FROM dbo.Team t
    --Season TBC
    INNER JOIN dbo.Fixture f ON t.TeamID IN (f.HomeTeamID, f.AwayTeamID)
    GROUP BY f.LeagueID, t.TeamName

以下是输出内容:

enter image description here

我希望在视图中包含另一个字段,其中包含每个团队的联赛位置。我的问题是在使用ROW_NUMBER时使用ORDER BY,因为我不确定要将其设置为什么,因为我无法将其设置为“点DESC&#39;和&#39; Goal_Difference DESC&#39;因为我在同一个SELECT语句中使用它来创建它。我的问题是如何在下面的代码中设置ORDER BY以输出正确的联赛位置?

ROW_NUMBER() OVER (PARTITION BY LeagueID ORDER BY ...)  AS Position,

2 个答案:

答案 0 :(得分:3)

再做一次选择

select *, ROW_NUMBER() OVER (PARTITION BY LeagueID ORDER BY Points DESC, Goal_Difference DESC)  AS Position
    from <your select statement>

答案 1 :(得分:3)

最简单的方法可能是子查询(派生表):

CREATE VIEW League_Table AS
select 
  data.*, 
  row_number() over (partition by leagueid 
                     order by points desc, goal_difference desc) as position,
from
(
  SELECT f.LeagueID, t.TeamName, 
  SUM(
      CASE WHEN f.HomeScore IS NOT NULL THEN 1 ELSE 0 END
     ) AS Played,
  SUM(
  ...
  GROUP BY f.LeagueID, t.TeamName
) data