2017-09-05 82 views
-1

我需要組合多行。
每一行作爲一個ID和一個子ID。
ID標識用戶,Sub ID標識合同期限。我需要結合基於這些行搶​​等欄目SQL - 將不同數量的行與匹配ID結合起來

ID  SubID StartDate StartVal EndDate 
123456 1  37622  100  37686 
123456 2  37781  20   37782 
254896 22  38584  44   38658 
254896 23  38722  18   38766 
254896 24  38825  6   38890 
254896 25  38972  68   39011  
365897 1  39696  38   41191 

和我的目標

ID  StartDate1 StartVal1 EndDate1 EndValue1 StartDate2 StartVal2 EndDate2 EndValue2.... 
123456 37622  100  37686 26  37781  20   37782 65     
254896 38584  44  38658 59  38722  18   38766 20.... 
365897 39696  38  41191 71        

ID  StartDate    StartVal EndDate     EndValue 
123456 37622,37688    81,8  37637,37722    12,90 
254896 38584,38740,38796,38836 98,22,68,10 38670,38788,38796,38925 6,10,31,57 
365897 39696     7   41191     74 

有什麼建議?在先進的感謝,薩姆

+2

我的建議是谷歌'SQL動態PIVOT多個columns' –

+0

好的,謝謝。我會看一下。 – bms45

+0

您正在使用哪個[DBMS](https://en.wikipedia.org/wiki/DBMS)產品? Postgres的?甲骨文? –

回答

0

您可以使用加入這個:

Select * from USER join ContactPeriod ON User."some common attribute of both tables" ="Contact.that attribute"