SQL Pivot麻烦

时间:2015-06-01 13:21:33

标签: sql sql-server-2008 pivot

我的数据看起来像这样:

+--------+-------------------------------+-------------+---------+-------------+----------------+--------------+-------------+--------------+--------------+-----------------+
|  Type  |          Description          | Issuer Code | Nominal | book_value  | profit_or_loss | market_value |  exposure   | average_cost | market_price | unrealised_gain |
+--------+-------------------------------+-------------+---------+-------------+----------------+--------------+-------------+--------------+--------------+-----------------+
| Issuer | BHP Billiton Plc              | BIL         |    1880 | 56073577.75 | NULL           | 55588623.09  | 55588623.09 | 265.486067   | 263.19       | -484954.66      |
| Issuer | Investec Limited              | INL         |    5300 | 14000040642 | NULL           | 28468872315  | 28468872315 | 57.536692    | 117          | 14468831673     |
| Issuer | Topi 40 Futures 15 March 2010 | ALSIH1      |       4 | NULL        | NULL           | NULL         | NULL        | NULL         | NULL         | NULL            |
+--------+-------------------------------+-------------+---------+-------------+----------------+--------------+-------------+--------------+--------------+-----------------+

我试图让它看起来像这样:

Type = Issuer
Description = BHP Billiton Plc
Issuer Code = BIL   
Nominal = 1880  
book_value = 56073577.75    
profit_or_loss = NULL   
market_value = 55588623.09  
exposure = 55588623.09  
average_cost = 265.486067000000 
market_price = 263.190000000000 
unrealised_gain = -484954.66


Type = Issuer
Description = Investec Limited
Issuer Code = INL   
Nominal = 5300  
book_value = 14000040641.84
profit_or_loss = NULL   
market_value = 28468872315.00   
exposure = 28468872315.00   
average_cost = 57.536692000000  
market_price = 117.000000000000 
unrealised_gain = 14468831673.16

我尝试使用PIVOT,但我完全不知道如何实现这一点。任何链接或教程都会很棒。

这是我对SQL的尝试(不是因为我不知道我在做什么,所以它有所帮助。呵呵)

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

SELECT tb.* INTO #TmpFDH FROM 
(
    SELECT
          instrument_id
         ,date_value
         ,book_value
         ,profit_or_loss
         ,market_value
         ,exposure
         ,average_cost
         ,market_price
         ,unrealised_gain
         ,ROW_NUMBER() OVER ( PARTITION  BY fdh.instrument_id order by d.date_value DESC) as rn
    FROM dw.Fact_Daily_Holding fdh
    JOIN dw.dim_date d 
        ON fdh.holding_date_id = d.date_id
    WHERE convert(date,date_value) = convert(date,GETDATE()-1)
    ) tb
WHERE tb.rn = 1



SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.instrument_id) 
            FROM #TmpFDH c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Type, ' + @cols + ' from 
            (
                    SELECT
                         i.instrument_id
                        ,rp.related_party_type AS [Type]--TYPE?
                        ,rp.related_party_name AS [Description]--DESCRIPTION?
                        ,rp.related_party_code AS [Issuer Code]--ISSUER CODE?
                        ,ISNULL(fo.traded_nominal,0) AS [Nominal]
                        ,fdh.book_value
                        ,fdh.profit_or_loss
                        ,fdh.market_value
                        ,fdh.exposure
                        ,fdh.average_cost
                        ,fdh.market_price
                        ,fdh.unrealised_gain
                    FROM dw.Dim_Instrument i
                    JOIN dw.Dim_Related_Party rp 
                        ON i.instrument_code = rp.related_party_code
                    LEFT JOIN dw.Fact_Order fo
                        ON i.instrument_id = fo.instrument_id
                    LEFT JOIN #TmpFDH fdh
                        ON i.instrument_id = fdh.instrument_id
           ) x
            pivot 
            (
                 max(book_value)
                for instrument_id in (' + @cols + ')
            ) p '


execute(@query)

drop table #TmpFDH

1 个答案:

答案 0 :(得分:1)

  1. 我同意评论,在xml中可能有更好的解决方案,因此也可能是一个选项
  2. 我很抱歉你的许多专栏都是保留字。
  3. 您可能正在寻找的是UNPIVOT,如下所示(根据示例中的列名称):

    SELECT * FROM mytable UNPIVOT ( value FOR label IN ([Type], [Description], [Issuer Code], [Nominal], [book_value], [profit_or_loss], [market_value], [exposure], [average_cost], [market_price], [unrealised_gain]) ) AS unpivot