2013-09-28 176 views
0

嗨,我有一個格式問題。我有一個人口稠密的桌子。該表是基於行的。不過,我需要將表格輸出爲基於列。我想我需要一個支點?SQL Server 2008 R2 - 樞軸用法?

我有這個表

CREATE TABLE [dbo].[phasepivot](
    [pinterval] [varchar](10) NULL, 
    [phasetype] [smallint] NULL, 
    [abegdate] [date] NULL, 
    [abegdatehr] [smallint] NULL, 
    [penddate] [date] NULL, 
    [pendhour] [smallint] NULL, 
    [alength] [int] NULL, 
    [avglength] [money] NULL, 
    [achgprevhilo] [money] NULL, 
    [avgchgprev] [money] NULL, 
    [achgprevhilopct] [money] NULL, 
    [avgchgprevpct] [money] NULL, 
    [adatehilo] [date] NULL, 
    [ahilovalue] [money] NULL, 
    [philovalue] [money] NULL, 
    [aperiodstohilo] [int] NULL, 
    [avgperiodstohilo] [money] NULL, 
    [anumhilos] [int] NULL, 
    [pnumhilos] [money] NULL, 
    [projphaselen] [varchar](50) NULL, 
    [recperiodstohilo] [int] NULL, 
    [recchgprevhilo] [money] NULL, 
    [recchgprevhilopct] [money] NULL, 
    [reclength] [money] NULL, 
    [avalue] [money] NULL, 
    [LTL] [money] NULL, 
    [UTL] [money] NULL, 
    [nxtphstype] [smallint] NULL, 
    [nxtphsdate] [date] NULL, 
    [nxtphshour] [smallint] NULL, 
    [nxtphshilo] [money] NULL 
) ON [PRIMARY] 

It contains this data 

pinterval phasetype abegdate abegdatehr penddate pendhour alength avglength achgprevhilo avgchgprev achgprevhilopct avgchgprevpct adatehilo ahilovalue philovalue aperiodstohilo avgperiodstohilo anumhilos pnumhilos projphaselen recperiodstohilo recchgprevhilo recchgprevhilopct reclength avalue LTL UTL nxtphstype nxtphsdate nxtphshour nxtphshilo 
H 1 2013-09-26 3 2013-09-27 1 12 6.00 -16.74 -20.9827 -0.0098 -0.0153 2013-09-27 1687.11 1682.8673 6 5.00 4 3.00 extended 3 -20.9827 -0.0153 6.4786 1691.75 1690.7904 1694.8193 2 2013-09-27 2 1709.5498 
D 1 2013-09-25 0 2013-10-01 0 3 5.00 -37.98 -55.4949 -0.0219 -0.0442 2013-09-25 1687.11 1571.9751 1 3.00 2 3.00 short 23 -425.32 -0.3419 46.00 1698.67 1691.4011 1704.1116 2 2013-10-08 0 1636.3851 
W 2 2013-09-09 0 2013-11-18 0 3 11.00 97.18 129.0572 0.0596 0.116 2013-09-16 1725.23 1757.1072 1 8.00 2 7.00 average 41 414.5201 0.6668 74.00 1691.75 1655.5985 1688.9805 1 2013-11-25 0 1618.5959 
M 2 2012-01-03 0 2013-02-04 0 21 14.00 501.7101 330.8972 0.4192 0.3409 2013-07-01 1698.4301 1527.6172 18 11.00 11 8.00 extended 32 683.73 1.4888 44.00 1691.75 1531.6484 1616.1792 1 2013-03-04 0 1462.0884 

I want the above table contents to look like this: 
pinterval H D W M 
phasetype 1 1 2 2 
abegdate 09/26/13 09/25/13 09/09/13 01/03/12 
abegdatehr 3 0 0 0 
penddate 09/27/13 10/01/13 11/18/13 02/04/13 
pendhour 1 0 0 0 
alength 12 3 3 21 
avglength 6 5 11 14 
achgprevhilo -16.74 -37.98 97.18 501.7101 
avgchgprev -20.9827 -55.4949 129.0572 330.8972 
achgprevhilopct -0.98% -2.19% 5.96% 41.92% 
avgchgprevpct -1.53% -4.42% 11.60% 34.09% 
adatehilo 09/27/13 09/25/13 09/16/13 07/01/13 
ahilovalue 1687.11 1687.11 1725.23 1698.4301 
philovalue 1682.8673 1571.9751 1757.1072 1527.6172 
aperiodstohilo 6 1 1 18 
avgperiodstohilo 5 3 8 11 
anumhilos 4 2 2 11 
pnumhilos 3 3 7 8 
projphaselen extended short average extended 
recperiodstohilo 3 23 41 32 
recchgprevhilo -20.9827 -425.32 414.5201 683.73 
recchgprevhilopct -1.53% -34.19% 66.68% 148.88% 
reclength 6.4786 46 74 44 
avalue 1691.75 1698.67 1691.75 1691.75 
LTL 1690.7904 1691.4011 1655.5985 1531.6484 
UTL 1694.8193 1704.1116 1688.9805 1616.1792 
nxtphstype 2 2 1 1 
nxtphsdate 09/27/13 10/08/13 11/25/13 03/04/13 
nxtphshour 2 0 0 0 
nxtphshilo 1709.5498 1636.3851 1618.5959 1462.0884 

難道我用透視?如果是,語法是什麼?

謝謝!

+0

你必須制定比你做的方式更多。 「H D W M」是什麼意思?以表格形式提供足夠的樣本數據以說明您已發佈的期望結果。您可以閱讀有關PIVOT和UNPIVOT語法[這裏](http://technet.microsoft.com/en-us/library/ms177410(v = sql.105).aspx) – peterm

+0

thx peterm - H =小時,D =天,W =周,M =月。沒有笑話,這就是表格中的所有數據。我對Pivot有些熟悉,但不知道如何使用它,當我不需要聚合函數時,我只需要像上面那樣翻轉的行/列。 –

回答

1

要實現這一點,您首先需要UNPIVOT您的表,然後再PIVOT。當你使用不同的數據類型轉換列時,其中一個複雜情況是你必須將它們全部轉換爲相同的數據類型,並且很可能會轉換其中的一些數據類型(例如日期,百分比等)。在以下示例中,VARCHAR(32)用作常用數據類型。您可能需要根據需要進行調整。

這裏是一個工作的查詢,只有你的一些列:

WITH unpivot_phase AS 
(
    SELECT property, pinterval, value 
    FROM 
    ( 
    SELECT pinterval, 
      CAST(phasetype  AS VARCHAR(32)) phasetype, 
      CAST(abegdate  AS VARCHAR(32)) abegdate, 
      CAST(abegdatehr  AS VARCHAR(32)) abegdatehr, 
      CAST(penddate  AS VARCHAR(32)) penddate, 
      CAST(pendhour  AS VARCHAR(32)) pendhour, 
      CAST(alength   AS VARCHAR(32)) alength, 
      CAST(avglength  AS VARCHAR(32)) avglength, 
      CAST(achgprevhilo AS VARCHAR(32)) achgprevhilo, 
      CAST(avgchgprev  AS VARCHAR(32)) avgchgprev, 
      CAST(achgprevhilopct AS VARCHAR(32)) achgprevhilopct, 
      CAST(avgchgprevpct AS VARCHAR(32)) avgchgprevpct, 
      CAST(adatehilo  AS VARCHAR(32)) adatehilo 
      -- add rest of the columns here 
     FROM phasepivot 
) s 
    UNPIVOT 
    ( 
    value FOR property IN (phasetype, abegdate, abegdatehr, 
          penddate, pendhour, alength, avglength, 
          achgprevhilo, avgchgprev, achgprevhilopct, 
          avgchgprevpct, adatehilo) -- add rest of the columns here 
) u 
) 
SELECT property, H, D, W, M 
    FROM 
(
    SELECT property, pinterval, value 
    FROM unpivot_phase 
) s 
PIVOT 
(
    MAX(value) FOR pinterval IN (H, D, W, M) 
) p 
ORDER BY 
    CASE property  
    WHEN 'phasetype'  THEN 1 
    WHEN 'abegdate'  THEN 2 
    WHEN 'abegdatehr'  THEN 3 
    WHEN 'penddate'  THEN 4 
    WHEN 'pendhour'  THEN 5 
    WHEN 'alength'   THEN 6 
    WHEN 'avglength'  THEN 7 
    WHEN 'achgprevhilo' THEN 8 
    WHEN 'avgchgprev'  THEN 9 
    WHEN 'achgprevhilopct' THEN 10  
    WHEN 'avgchgprevpct' THEN 11 
    WHEN 'adatehilo'  THEN 12 
    END 

輸出:

 
|  PROPERTY |   H |   D |   W |   M | 
|-----------------|------------|------------|------------|------------| 
|  phasetype |   1 |   1 |   2 |   2 | 
|  abegdate | 2013-09-26 | 2013-09-25 | 2013-09-09 | 2012-01-03 | 
|  abegdatehr |   3 |   0 |   0 |   0 | 
|  penddate | 2013-09-27 | 2013-10-01 | 2013-11-18 | 2013-02-04 | 
|  pendhour |   1 |   0 |   0 |   0 | 
|   alength |   12 |   3 |   3 |   21 | 
|  avglength |  6.00 |  5.00 |  11.00 |  14.00 | 
| achgprevhilo |  -16.74 |  -37.98 |  97.18 |  501.71 | 
|  avgchgprev |  -20.98 |  -55.49 |  129.06 |  330.90 | 
| achgprevhilopct |  -0.98 |  -2.19 |  5.96 |  41.92 | 
| avgchgprevpct |  -1.53 |  -4.42 |  11.60 |  34.09 | 
|  adatehilo | 2013-09-27 | 2013-09-25 | 2013-09-16 | 2013-07-01 | 

這裏是SQLFiddle演示

+0

peterm - 輝煌!有效。謝謝!只是最後一個nit ...旋轉表按屬性alpha asc排序。需要從我的原始帖子(手動)旋轉表中的屬性順序。顯然,我可以添加一個列到相位主軸,其中包括一個整數排序。然而,我很難理解在最後一個SQL帖子底部的PIVOT選擇語句中將SORT BY放在哪裏? –

+1

@FrankZappa你很歡迎:)查看更新的答案和sqlfiddle示例。保持正確順序的兩岸前進方法只需在'CASE ... END'中添加'ORDER BY'。如果您覺得答案有幫助,請考慮** [接受](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)**。 – peterm