合并两个查询且没有任何关系

时间:2018-11-25 14:34:42

标签: sql-server merge

我正在SQL Server中创建一个存储过程,其中有2个不同的查询选择不同的记录但具有相同的表,并且两者在查询中都没有任何关系。

这是我的查询

SELECT * 
FROM
    (SELECT TOP 3
         Players.Player_Name AS 'HomeTeamBatsman',
         PlayerScores.Bat_Runs As 'HomeTeamRuns'        
     FROM 
         PlayerScores
     INNER JOIN
         Players ON PlayerScores.PlayerId = Players.PlayerId
     INNER JOIN
         Teams ON Players.TeamId = Teams.TeamId
     INNER JOIN
         Matches ON PlayerScores.MatchId = Matches.MatchId
     WHERE 
         (Teams.TeamId = 1)
         AND (Matches.MatchId = 1025)
     GROUP BY 
         Players.Player_Name,
         PlayerScores.Bat_Runs
     ORDER BY
         MAX(Bat_Runs) DESC) As HomeTeamBatting,
    (SELECT TOP 3
         Players.Player_Name AS 'OpponentTeamBatsman',
         PlayerScores.Bat_Runs As 'OpponentTeamRuns'     
     FROM 
         PlayerScores
     INNER JOIN
         Players ON PlayerScores.PlayerId = Players.PlayerId
     INNER JOIN
         Teams ON Players.TeamId = Teams.TeamId
     INNER JOIN
         Matches ON PlayerScores.MatchId = Matches.MatchId
     WHERE 
         (Teams.TeamId = 3)
         AND (Matches.MatchId = 1025)
     GROUP BY 
         Players.Player_Name,
         PlayerScores.Bat_Runs
     ORDER BY
         MAX(Bat_Runs) DESC) AS OpponentTeamBatting
END
go

它返回以下结果,但是,我不希望这些查询有任何联系

Image

我期望的是

enter image description here

1 个答案:

答案 0 :(得分:3)

一个纯粹的猜测,我 认为 ,你想UNION ALL

SELECT *
FROM (
    SELECT TOP 3,
           'Home' AS Team,
           P.Player_Name AS Batsman,
           PS.Bat_Runs As Runs  
    FROM PlayerScores PS
         JOIN Players P ON PS.PlayerId = P.PlayerId
         JOIN Teams T ON P.TeamId = T.TeamId
         JOIN Matches M  ON PS.MatchId = M.MatchId
    WHERE T.TeamId = 1
    AND M.MatchId = 1025
    GROUP BY P.Player_Name,
             PS.Bat_Runs
    ORDER BY Bat_Runs DESC --As the GROUP BY is on Bat_Runs, MAX isn't needed
                           --In truth,why is there even a GROUP BY with no aggregation?
) AS Home
UNION ALL
SELECT *
FROM (
    SELECT Top 3
           'Away' AS Team,
           P.Player_Name AS Batsman,
           PS.Bat_Runs As Runs
    FROM PlayerScores PS
         JOIN Players P ON PS.PlayerId = P.PlayerId
         JOIN Teams T ON P.TeamId = T.TeamId
         JOIN Matches M  ON PS.MatchId = M.MatchId

    WHERE T.TeamId = 3
      AND Matches.MatchId = 1025
    GROUP BY P.Player_Name,
             PS.Bat_Runs
    ORDER BY Bat_Runs DESC
    ) AS Away;

编辑:现在,我们有了一个预期的结果集。我怀疑这里不需要GROUP BY。为什么?好吧,OP没有聚合功能,因此将其放置在这里是没有意义的,因此我将其删除。

他们说他们没有关系,但是有一个关系,他们想要的关系在两支球队的得分上。因此,这给出了如下查询:

WITH Home AS(
    SELECT P.Player_Name AS Batsman,
           PS.Bat_Runs As Runs,
           RANK() OVER (ORDER BY PS.Bat_Runs) AS RunsRank
    FROM PlayerScores PS
         JOIN Players P ON PS.PlayerId = P.PlayerId
         JOIN Teams T ON P.TeamId = T.TeamId
         JOIN Matches M  ON PS.MatchId = M.MatchId
    WHERE T.TeamId = 1
    AND M.MatchId = 1025),
Away AS (
    SELECT P.Player_Name AS Batsman,
           PS.Bat_Runs As Runs,
           RANK() OVER (ORDER BY PS.Bat_Runs) AS RunsRank
    FROM PlayerScores PS
         JOIN Players P ON PS.PlayerId = P.PlayerId
         JOIN Teams T ON P.TeamId = T.TeamId
         JOIN Matches M  ON PS.MatchId = M.MatchId
    WHERE T.TeamId = 3
      AND Matches.MatchId = 1025
    )
SELECT H.Batsman AS HomeBatsMan,
       H.Runs AS HomeBatsManRuns,
       A.Batsman AS AwayBatsMan,
       A.Runs AS AwayBatsManRuns
FROM Home H
     JOIN Away A ON H.RunsRank = A.RunsRank
WHERE H.RunsRank <= 3;