2009-07-08 40 views
1

,我從檢索數據的數據庫具有表結構這樣逆透視主場迎戰聯盟查詢在T-SQL 2008

表:ClientSales

ClientSalesId     int identity (1, 1) (PK) 
ClientId      int (FK) 
TermId      int (FK) 
StudentType1Population  int 
StudentType1Adjustment  int 
StudentType1Sales    int 
StudentType1SalesAdjustment int 
StudentType2Population  int 
StudentType2Adjustment  int 
StudentType2Sales    int 
StudentType2SalesAdjustment int 
StudentType3Population  int 
StudentType3Adjustment  int 
StudentType3Sales    int 
StudentType3SalesAdjustment int 
StudentType4Population  int 
StudentType4Adjustment  int 
StudentType4Sales    int 
StudentType4SalesAdjustment int 
StudentType5Population  int 
StudentType5Adjustment  int 
StudentType5Sales    int 
StudentType5SalesAdjustment int 

我要顯示它在一份報告中unpivot操作如下

對於客戶端Id = 1和期限ID = 1

 
       Population PopulationAdjustment Sales SalesAdjustment 
StudentType1 313   18     123 22 
StudentType2 233   14     156 33 
StudentType3 234   12     112 41 
StudentType4 233   13     198 29 
StudentType5 343   10     134 36 

我能做到這兩個方面


SELECT 
     'StudentType1'    as DemographicType 
     StudentType1Population  as Population, 
     StudentType1Adjustment  as PopulationAdjustment, 
     StudentType1Sales   as Sales, 
     StudentType1SalesAdjustment as SalesAdjustment, 
FROM ClientSales 
WHERE 1=1 
     AND ClientId = 1 
     AND TermId = 1 

UNION 

SELECT 
     'StudentType2'    as DemographicType 
     StudentType2Population  as Population, 
     StudentType2Adjustment  as PopulationAdjustment, 
     StudentType2Sales   as Sales, 
     StudentType2SalesAdjustment as SalesAdjustment, 
FROM ClientSales 
WHERE 1=1 
     AND ClientId = 1 
     AND TermId = 1 

-- yada yada yada for the rest of the types........... 

OR


SELECT 
     ClientId, 
     Population 
FROM 
(
     SELECT 
       ClientId, 
       StudentType1Population, 
       StudentType2Population, 
       StudentType3Population, 
       StudentType4Population, 
       StudentType5Population 
     FROM ClientSales 
) PVTPopulation 
UNPIVOT 
(
     Population for StudentType IN 
     (
       StudentType1Population, 
       StudentType2Population, 
       StudentType3Population, 
       StudentType4Population, 
       StudentType5Population 
     ) 
) as UnPvtPopulation 

INNER JOIN 

(
     SELECT 
       ClientId, 
       StudentType1PopulationAdjustment, 
       StudentType2PopulationAdjustment, 
       StudentType3PopulationAdjustment, 
       StudentType4PopulationAdjustment, 
       StudentType5PopulationAdjustment 
     FROM ClientSales 
) PVTPopulation 
UNPIVOT 
(
     PopulationAdjustment for StudentType IN 
     (
       StudentType1PopulationAdjustment, 
       StudentType2PopulationAdjustment, 
       StudentType3PopulationAdjustment, 
       StudentType4PopulationAdjustment, 
       StudentType5PopulationAdjustment 
     ) 
) as UnPvtPopulationAdjustment 

     ON UnPvtPopulationAdjustment.ClientSalesId = UnPvtPopulation.ClientSalesId 
     AND REPLACE (UnPvtPopulationAdjustment.StudentType, 'PopulationAdjustment', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '') 

INNER JOIN 

(
     SELECT 
       ClientId, 
       StudentType1Sales, 
       StudentType2Sales, 
       StudentType3Sales, 
       StudentType4Sales, 
       StudentType5Sales 
     FROM ClientSales 
) PVTSales 
UNPIVOT 
(
     Sales for StudentType IN 
     (
       StudentType1Sales, 
       StudentType2Sales, 
       StudentType3Sales, 
       StudentType4Sales, 
       StudentType5Sales 
     ) 
) as UnPvtSales 

     ON UnPvtSales.ClientSalesId = UnPvtPopulation.ClientSalesId 
     AND REPLACE (UnPvtSales.StudentType, 'Sales', '') = REPLACE (UnPvtPopulation.StudentType, 'Population', '') 

所以這裏的問題:

  • 作爲最佳實踐,我應該使用UNPIVOT或UNION
  • 有沒有更好的方法來寫這個UNPIVOT?

回答

5

我發現一個UNPIVOT就像一個where子句..你可以用,因爲你需要儘可能多的

SELECT Col1, Col2, Unp1, Unp2, Unp3 
FROM TBL 
UNPIVOT (XX For Unp1 (ColXX1, ColXX2)) 
UNPIVOT (YY For Unp2 (ColYY1, ColYY2)) 
UNPIVOT (ZZ For Unp2 (ColZZ1, ColZZ2))