2017-09-29 70 views
2

我有一個表具有以下數據SQL Server的多支點

+-----------+-----------+----------+----------+--------+ 
| SourceZip | TargetZip | TargetID | Distance | rowNum | 
+-----------+-----------+----------+----------+--------+ 
|  00602 |  08831 | 14780 | 1567.579 | R1  | 
|  00602 |  08831 |  6398 | 1567.579 | R2  | 
|  00602 |  07747 |  4290 | 1570.665 | R3  | 
|  00603 |  08831 | 14780 | 1561.272 | R1  | 
|  00603 |  08831 |  6398 | 1561.272 | R2  | 
|  00603 |  07747 |  4290 | 1564.328 | R3  | 
+-----------+-----------+----------+----------+--------+ 

我需要這個以下列格式

+-----------+-------------+-------------+-------------+-------------+-------------+-------------+ 
| SourceZip | R1_TargetID | R1_Distance | R2_TargetID | R2_Distance | R3_TargetID | R3_Distance | 
+-----------+-------------+-------------+-------------+-------------+-------------+-------------+ 
|  00602 |  14780 | 1567.579 |  6398 | 1567.579 |  4290 | 1570.665 | 
|  00603 |  14780 | 1561.272 |  6398 | 1561.272 |  4290 | 1564.328 | 
+-----------+-------------+-------------+-------------+-------------+-------------+-------------+ 

有人告訴我,我可以用一個支點此,但是我可以找到的每個示例主軸都使用一個聚合函數,並且只爲旋轉數據生成一列。

這是我可以做一個單一的樞紐,或者我需要生成2個單獨的數據透視表(TargetID和距離各一個),然後加入他們嗎?

UPDATE

我結束了該數據被傳遞,我需要爲一個值的字段,可以在應用程序中解析出結合到

select * 
from (SELECT SourceZip, CONVERT(varchar(10), TargetID)+ '|' + CONVERT(varchar(10), Distance) as TargetSite, rowNum 
     FROM Targets 
    ) as SourceTable 

pivot (min(TargetSite) 
for rowNum in (R1 
       ,R2 
       ,R3 
       ) 
    ) as pivottable 

它的效果並不理想,但它的作品。

我仍然欣賞了多列旋轉任何建議

回答

2

如果它可能轉換樞轉到同一類型的列,你可以使用這種方法。

DECLARE @MyTable TABLE (SourceZip VARCHAR(10), TargetZip VARCHAR(10),TargetID INT, Distance VARCHAR(10), rowNum VARCHAR(10)) 

INSERT INTO @MyTable VALUES 
('00602','08831','14780','1567.579','R1'), 
('00602','08831','6398','1567.579','R2'), 
('00602','07747','4290','1570.665','R3'), 
('00603','08831','14780','1561.272','R1'), 
('00603','08831','6398','1561.272','R2'), 
('00603','07747','4290','1564.328','R3') 

SELECT * FROM 
    (
     SELECT SourceZip, 
      CONVERT(VARCHAR(20),TargetID) [TargetID_Distance], 
      rowNum + '_TargetID' rowNum 
      FROM @MyTable 
     UNION ALL 
     SELECT SourceZip, 
      CONVERT(VARCHAR(20),Distance) [TargetID_Distance], 
      rowNum + '_Distance' rowNum 
     FROM @MyTable 
    ) SRC 
    PIVOT (MAX([TargetID_Distance]) 
      FOR rowNum IN ([R1_TargetID], 
          [R1_Distance], 
          [R2_TargetID], 
          [R2_Distance], 
          [R3_TargetID], 
          [R3_Distance])) PVT 

結果:

SourceZip R1_TargetID   R1_Distance   R2_TargetID   R2_Distance   R3_TargetID   R3_Distance 
---------- -------------------- -------------------- -------------------- -------------------- -------------------- -------------------- 
00602  14780    1567.579    6398     1567.579    4290     1570.665 
00603  14780    1561.272    6398     1561.272    4290     1564.328 
+0

謝謝,這是做到了。我最終把它們都當成了花車而不是變種,而且它像一個魅力一樣工作。 – Broom

1

所以,你要每SourceZip一行:

... 
GROUP BY SourceZip; 

現在,你想R1_TargetID柱:

SELECT 
    SUM(CASE WHEN rowNum = 'R1' THEN TargetID END) R1_TargetID, 

casenull替換不是rowNum = 'R1'的所有內容(由於暗含的else null條款)。 sum將忽略null。只要只有一行有rowNum = 'R1',它將與原始值的結果相同。

其他列同樣可以做到:

SUM(CASE WHEN rowNum = 'R1' THEN Distance END) R1_Distance 
... 

更多關於這項技術:http://modern-sql.com/use-case/pivot

+0

upvoted的方法和優秀的資源鏈接。謝謝你教我一些新東西,即使我最終沒有使用這種方法 – Broom