选择查询以获取表的数据的不同表示

时间:2014-05-22 11:54:33

标签: sql-server

我有一张如下图所示的表格" source"。

LoadNo | CostType | CostTotal | CostRate
-------+----------+-----------+---------
   100 | LHS      |      1000 |        4
   100 | FS       |      2000 |        3
   100 | STP      |      3000 |        5
   200 | LHS      |      4000 |        1
   200 | FS       |      1200 |        5
   200 | STP      |      3200 |        2

在此表中,有三种CostType,即LHS,FS和STP。现在对于每个LoadNo,它将具有全部三个CostType,并且对于每个CostType,它将具有CostTotal和CostRate(例如,对于表中的LoadNo 100,它具有所有三个CostTypes LHS,FS和STP以及每个CostType的CostTotal和CostRate)。现在我试图在这个表上查询我可以像bellow table一样检索输出

LoadNo | LHSCost | FSCost | STPCost | LHSRate | FSRate | STPRate 
-------+---------+--------+---------+---------+--------+---------
   100 |    1000 |   2000 |    3000 |       4 |      3 |       5 
   200 |    4000 |   1200 |    3200 |       1 |      5 |       2 

在此o / p表中,loadNo与源表相同,但此处源表的CostType(LHS,FS,STP以及每个CostType具有CostTotal和CostRate)是与CostTotal和CostRate组合的变为列。所以现在列是LHSCost,LHSRate,FSCost等。所以,请帮我找一个查询/ sp来获得这样的输出。

2 个答案:

答案 0 :(得分:2)

另一种方法是使用PIVOT

Select LoadNo
     , LHSCost, FSCost, STPCost
     , LHSRate, FSRate, STPRate
From   (Select LoadNo
             , CostType + 'Cost' Type
             , CostTotal Value
        From   Table1
        UNION ALL
        Select LoadNo
             , CostType + 'Rate' Type
             , CostRate Value
        From   Table1) d
        PIVOT (AVG(Value) For Type IN (LHSCost, FSCost, STPCost
                                     , LHSRate, FSRate, STPRate)) pvt

或者你可以使用聚合伪装它

Select LoadNo
     , LHSCost = SUM(Case When CostType = 'LHS' Then CostTotal Else 0 End)
     , FSCost  = SUM(Case When CostType = 'FS'  Then CostTotal Else 0 End)
     , STPCost = SUM(Case When CostType = 'STP' Then CostTotal Else 0 End)
     , LHSRate = SUM(Case When CostType = 'LHS' Then CostRate  Else 0 End)
     , FSRate  = SUM(Case When CostType = 'FS'  Then CostRate  Else 0 End)
     , STPRate = SUM(Case When CostType = 'STP' Then CostRate  Else 0 End)
From   Table1
Group By LoadNo

答案 1 :(得分:1)

希望这就是你要找的东西,

DECLARE @t TABLE 
(LoadNo int,CostType Char(4),CostTotal Int,CostRate int)

INSERT INTO @T VALUES
(100,'LHS',1000,4),(100,'FS',2000,3),
(100,'STP',3000,5),(200,'LHS',4000,1),
(200,'FS',1200,5),(200,'STP',3200,2)

SELECT * FROM @t

enter image description here

SELECT  LHS.LoadNo,
        LHS.CostTotal LHSCost,
        FS.CostTotal FSCost,
        STP.CostTotal STPCost,
        LHS.CostRate LHSRate,
        FS.CostRate FSRate,
        STP.CostRate CRate
FROM    (
        SELECT  LoadNo,
                MAX(CostTotal) CostTotal,
                MAX(CostRate) CostRate 
        FROM    @t 
        WHERE   CostType = 'LHS'
        GROUP BY LoadNo) LHS
JOIN    (
        SELECT  LoadNo,
                MAX(CostTotal) CostTotal,
                MAX(CostRate) CostRate 
        FROM    @t 
        WHERE   CostType = 'FS'
        GROUP BY LoadNo) FS ON LHS.LoadNo = FS.LoadNo
JOIN    (
        SELECT  LoadNo,
                MAX(CostTotal) CostTotal,
                MAX(CostRate) CostRate 
        FROM    @t 
        WHERE   CostType = 'STP'
        GROUP BY LoadNo) STP ON LHS.LoadNo = STP.LoadNo

enter image description here