[英]Combine multiple rows into one row MySQL and split value from one field to two in same Query
[英]How to do in SQL: one row from two rows with same value?
我在 SQL 中有這樣的表:
我想從具有相同game_id
值的行創建簡單的行。 因此,我想在第一行中有前兩行; 第三、四排為第二排,以此類推。 我的桌子很大。 感謝您提前提供任何幫助。
您可以使用條件聚合:
select game_id,
max(case when seqnum = 1 then team_id end) as team_1,
max(case when seqnum = 1 then score end) as score_1,
max(case when seqnum = 2 then team_id end) as team_2,
max(case when seqnum = 2 then score end) as score_2
from (select t.*, row_number() over (partition by game_id order by game_id) as seqnum
from t
) t
group by game_id;
我應該注意到 SQL 表表示無序集,因此結果列的順序可能與您顯示的行的順序不同。 如果您有訂單,那么您可以按order by
使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.