2010-06-24 85 views
1

我要爲列而非行即透視SQL Server上2010

Owner_ID Pet 
-------------------- 
1  Cat 
1  Dog 
1  Hamster 
2  Cow 
2  Sheep 
3  Dog 

將它轉換爲每個員工返回前三名寵物

Owner_ID Pet1 Pet2 Pet3 
------------------------------------- 
1   Cat Dog Hamster 
2   Cow Sheep null 
3   Dog null null 

寵物的名字來自查找表可以有任意數量的寵物但我只想返回前三名。

這是我的查詢:

SELECT Owner,Pet1, Pet2,Pet3 
    FROM 
    (select distinct OwnerID as Owner,glcom.Value as Pets 
    from Owner ,OwnerPets ,Pet  
    where Pet.Type='Furry' 
    and OwnerPets.OwnerID = OwnerID.OwnerID 
    and OwnerPets.PetID = Pet.PetID) AS SourceTable 
PIVOT 
( 
Max(Pets) 
FOR Pets IN (Pet1, Pet2,Pet3) 
) AS PivotTable; 

遺憾的是它只爲每一行返回null ...所以我看到的輸出是

Owner_ID Pet1 Pet2 Pet3 
------------------------------------- 
1   null null null 
2   null null null 
3   null null null 

希望這是一個普遍的問題,必須有人已經解決了它。

感謝

+2

跆拳道是SQL Server 2010的?最新版本是2008 R2。 – 2010-06-24 22:48:08

+0

注意:我的答案假設您在跟蹤Brent Ozar調用20​​08 R2 SQL Server 2010! http://www.brentozar.com/archive/2009/02/sql-server-2010-features-leaked/ – 2010-06-25 08:36:21

+0

:)是的,它的確如此。 – sfomate 2010-06-28 16:28:29

回答

2

如果我明白你正確,你就可以使用旋轉那些在查詢中使用ROW_NUMBER() over (partition by Owner_ID order by ...)

示例如下。

WITH Unpivoted AS 
(
SELECT X.*, ROW_NUMBER() over (partition by Owner_ID order by Pet) AS RN 
    FROM (VALUES 
(1,  'Cat') , 
(1,  'Dog') , 
(1,  'Hamster') , 
(1,  'Rhino'), 
(1,  'Zebra'), 
(2,  'Cow') , 
(2,  'Sheep') , 
(3,  'Dog') 
) AS X (Owner_ID, Pet) 
) 

SELECT Owner_Id, [1] AS Pet1, [2] AS Pet2,[3] AS Pet3 FROM Unpivoted 
PIVOT 
( 
Max(Pet) 
FOR RN IN ([1], [2],[3]) 
) AS PivotTable; 

返回

Owner_Id Pet1 Pet2 Pet3 
----------- ------- ------- ------- 
1   Cat  Dog  Hamster 
2   Cow  Sheep NULL 
3   Dog  NULL NULL 
+0

我以前嘗試過行號,但遇到的問題是,如果業主有兩隻貓,比我需要得到一個獨特的和行號不起作用。 – sfomate 2010-06-28 16:27:27

+0

這有助於現在解決問題,稍後我會處理獨特的設置。 – sfomate 2010-06-28 17:23:42