2014-09-25 63 views
0

這可能很簡單,但是我有一個心智空白的atm。SQL在多行的同一個表之間進行連接

Table A 

ID  Seq Txt 
--------------------------- 
10  0  Orange 
10  1  Banana 
20  0  Mango 
30  0  Apple 
30  1  Grape 
30  2  Pineaple 

我希望能夠將所有ID分組並將它們顯示在一行上。

ID  Seq1 Txt1 Seq2 Txt2 
------------------------------------- 
10  0  Orange 1  Banana 
20  0  Mango null null 
.. 30 etc 

我怎麼去呢?我自己進行連接並對Seq編號進行OR操作?

感謝

回答

1

您可以通過case語句使用條件彙總:

select  id, 
      min(case seq when 0 then 0 end) as seq1, 
      min(case seq when 0 then txt end) as txt1, 
      min(case seq when 1 then 1 end) as seq2, 
      min(case seq when 1 then txt end) as txt2, 
      min(case seq when 2 then 2 end) as seq3, 
      min(case seq when 2 then txt end) as txt3 
from  tbl 
group by id 

但是你必須更case語句添加到上面,如果以次能夠超越#2

相關問題