2016-02-11 31 views
0

我有这个表结果CONCAT多个行​​与SQL

team_id | match_id | result 
---------------------------- 
1  |13  | 1-1 
1  |13  | 2-0 
1  |13  | 0-3 

我想改变它在这个

team_id | match_id | result | result 2 | result 3 
-------------------------------------------------- 
1  |13  | 1-1 | 2-0 | 0-3 
+1

它会一直是一个固定数量的匹配,或可以匹配的数量不同吗?另外,你是否真的需要在数据层做到这一点?在表现层做事可能会更容易。 – eftpotrm

+0

另外,有没有什么要订购的结果?如果没有,比赛可以以任何顺序出现。 –

+0

你如何确定最终结果值必须在结果栏1,2或3中? – Kobi

回答

1

如果排序是任意的,恰好有THRE,那么你可以使用有条件的聚合:

select team_id, max(case when seqnum = 1 then result end) as result1, 
     max(case when seqnum = 2 then result end) as result2, 
     max(case when seqnum = 3 then result end) as result3 
from (select t.*, 
      row_number() over (partition by team_id order by team_id) as seqnum 
     from t 
    ) t 
group by team_id; 
+0

thx,这是我想要的,但要做到这一点,我必须确定匹配的数量,但没关系;) – Shiroga