SQL 如何将行合二为一

SQL How to Combine Rows into One

我有来自查询的数据:

SELECT Match.MatchID, [Set].SetValue, Game.GameValue
FROM Game INNER JOIN
 [Set] ON Game.SetID = [Set].SetID INNER JOIN
 Match ON [Set].MatchID = Match.MatchID
 ORDER BY Match.MatchID, [Set].SetValue, Game.GameValue desc

我想要实现的是以下结果:

您可以使用条件聚合:

select s.matchid,
       concat(max(case when s.setvalue = 1 then g.gamevalue end), '-',
              min(case when s.setvalue = 1 then g.gamevalue end)
             ) as set1,
       concat(max(case when s.setvalue = 2 then g.gamevalue end), '-',
              min(case when s.setvalue = 2 then g.gamevalue end)
             ) as set2     
from Game g join
     [Set] s
     on g.SetID = s.SetID 
group by s.matchid;