嗨,我有一個格式問題。我有一個人口稠密的桌子。該表是基於行的。不過,我需要將表格輸出爲基於列。我想我需要一個支點?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
難道我用透視?如果是,語法是什麼?
謝謝!
你必須制定比你做的方式更多。 「H D W M」是什麼意思?以表格形式提供足夠的樣本數據以說明您已發佈的期望結果。您可以閱讀有關PIVOT和UNPIVOT語法[這裏](http://technet.microsoft.com/en-us/library/ms177410(v = sql.105).aspx) – peterm
thx peterm - H =小時,D =天,W =周,M =月。沒有笑話,這就是表格中的所有數據。我對Pivot有些熟悉,但不知道如何使用它,當我不需要聚合函數時,我只需要像上面那樣翻轉的行/列。 –