0

嗨,我有一个格式问题。我有一张被填充的桌子。该表是基于行的。但是我需要将表输出作为基于列的。我想我需要一个支点?

我有这张桌子

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

我使用枢轴吗?如果是,语法是什么?

谢谢!

4

1 回答 1

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

输出:

| 物业 | H | D | W | 中号 |
|-----------------|------------|------------|----- -------|------------|
| 相位类型 | 1 | 1 | 2 | 2 |
| 初步 | 2013-09-26 | 2013-09-25 | 2013-09-09 | 2012-01-03 |
| 阿贝格达 | 3 | 0 | 0 | 0 |
| 待定 | 2013-09-27 | 2013-10-01 | 2013-11-18 | 2013-02-04 |
| 未决时间 | 1 | 0 | 0 | 0 |
| 长度 | 12 | 3 | 3 | 21 |
| 平均长度 | 6.00 | 5.00 | 11.00 | 14.00 |
| 阿奇普雷维洛 | -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 |
| 阿达提洛 | 2013-09-27 | 2013-09-25 | 2013-09-16 | 2013-07-01 |

这是SQLFiddle演示

于 2013-09-28T04:41:25.943 回答