移调列使用UNPIVOT行(Transposing columns to rows using UN

2019-09-22 22:40发布

我有一个由于某种原因已经硬编码像这样值的表:

Row ID    QtyC1   QtyC2  QtyC3   QtyC4  QtyN1   QtyN2  QtyN3   QtyN4  
100       10      5      8       9      11      12     5       6
101       9       11     12      5      6       10     4       9

表中有35列,12K左右的记录(意味着500K左右的值),并且被添加到不断修正。

我想在一个视图到移调这样的:

Row ID  Year  Period  Val
100     C     1       10
100     C     2       5
100     C     3       8
100     C     4       9
100     N     1       11
100     N     2       12
100     N     3       5
100     N     4       6

到目前为止,我已经设法它拆分为使用该查询单值:

SELECT Row ID, YP, Val

FROM (SELECT Row ID
    , QtyC1 AS C1
    , QtyC2 AS C2
    , QtyC3 AS C3
    , QtyC4 AS C4
    , QtyN1 AS N1
    , QtyN2 AS N2
    , QtyN3 AS N3
    , QtyN4 AS N4

FROM MyTable
) SUB
UNPIVOT (Val FOR YP IN (C1,C2,C3,C4,N1,N2,N3,N4)) AS PVT

这让我一个确定值(例如C1 ),但我怎么可以拆分,所以我有一年(数字周期和单个字符1C )?

我可以看到有可能只是分裂了绳子分成两个部分,但我希望如果可能的话更清洁的方式。

Answer 1:

为什么这似乎不干净?

SELECT Row ID, left(YP, 1) as year, cast(right(yp, 1) as int) as period, Val
FROM (SELECT Row ID
    , QtyC1 AS C1
    , QtyC2 AS C2
    , QtyC3 AS C3
    , QtyC4 AS C4
    , QtyN1 AS N1
    , QtyN2 AS N2
    , QtyN3 AS N3
    , QtyN4 AS N4
FROM MyTable
) SUB
UNPIVOT (Val FOR YP IN (C1,C2,C3,C4,N1,N2,N3,N4)) AS PVT


Answer 2:

您可以轻松地分割YP使用字符串LEFT() RIGHT() SUBSTRING()等我的建议是,你是如何处理你的UNPIVOT 。 它看起来像你有很多的列到UNPIVOT所以我的建议可能是实现动态SQL执行此查询。 你会做这种方式:

declare @colsUnpivot varchar(max),
  @query  AS NVARCHAR(MAX),
  @cols  varchar(max)

select @colsUnpivot = stuff((select ','
                             +quotename(replace(C.name, 'Qty', ''))
         from sys.columns as C
         where C.object_id = object_id('yourtable') and
               C.name like 'Qty%'
         for xml path('')), 1, 1, '')

select @cols = stuff((select ','
                      +quotename(C.name) + ' as ' + replace(C.name, 'Qty', '')
         from sys.columns as C
         where C.object_id = object_id('yourtable') and
               C.name like 'Qty%'
         for xml path('')), 1, 1, '')

set @query 
  = 'select rowid, 
          left(YP, 1) YP,
          cast(right(YP, len(YP) - 1) as int) period,
          Val
     from
     (
        select rowid, ' + @cols + '
        from yourtable
     ) x1
     unpivot
     (
        val for YP IN (' + @colsUnpivot + ')
     ) u'

exec(@query)

看到SQL拨弄演示



文章来源: Transposing columns to rows using UNPIVOT