【问题标题】:Crosstab or transpose query交叉表或转置查询
【发布时间】:2014-03-06 04:38:18
【问题描述】:

我有一个这样的查询的结果集

mon-yar Count   EB      VC
Apr-11    34    1237    428
May-11    54    9834    87
Jun-11    23    9652    235
Jul-11    567   10765   1278
Aug-11    36    10234   1092
Sep-11    78    8799    987
Oct-11    23    10923   359
Nov-11    45    11929   346
Dec-11    67    9823    874
Jan-12    45    2398    245
Feb-12    90    3487    937
Mar-12   123    7532    689
Apr-12   109    1256    165

我希望是这样的:

monthyear   Apr-11  May-11  Jun-11  Jul-11  Aug-11  Sep-11  Oct-11  Nov-11  Dec-11  Jan-12  Feb-12  Mar-12  Apr-12
Count           34      54      23     567      36      78      23      45      67      45      90     123     109
EB            1237    9834    9652   10765   10234    8799   10923   11929    9823    2398    3487    7532    1256
VC             428      87     235    1278    1092     987     359     346     874     245     937     689     165

月份年份值是动态的。我该怎么做才能以这种方式生成它?

【问题讨论】:

  • @AmirrezaKeshavarz:动态列和多个项目列表的透视管理非常复杂。如果可以的话,我正在寻找更好的方法。
  • Excel 是一种以这种方式格式化表格的正确工具(又名数据透视表)
  • 你用的是什么数据库?

标签: sql sql-server-2008 tsql crosstab transpose


【解决方案1】:

如果您不想使用 PIVOT,可以使用下面的解决方案,只要您不介意在 Excel 中对结果使用文本到列。

如果你要跑步:

with tbl as(
select 'Apr-11' as monyar, 34 as cnt, 1237 as eb, 428 as vc from dual union all
select 'May-11' as monyar, 54 as cnt, 9834 as eb, 87 as vc from dual union all
select 'Jun-11' as monyar, 23 as cnt, 9652 as eb, 235 as vc from dual union all
select 'Jul-11' as monyar, 567 as cnt, 10765 as eb, 1278 as vc from dual union all
select 'Aug-11' as monyar, 36 as cnt, 10234 as eb, 1092 as vc from dual union all
select 'Sep-11' as monyar, 78 as cnt, 8799 as eb, 987 as vc from dual union all
select 'Oct-11' as monyar, 23 as cnt, 10923 as eb, 359 as vc from dual union all
select 'Nov-11' as monyar, 45 as cnt, 11929 as eb, 346 as vc from dual union all
select 'Dec-11' as monyar, 67 as cnt, 9823 as eb, 874 as vc from dual union all
select 'Jan-12' as monyar, 45 as cnt, 2398 as eb, 245 as vc from dual union all
select 'Feb-12' as monyar, 90 as cnt, 3487 as eb, 937 as vc from dual union all
select 'Mar-12' as monyar, 123 as cnt, 7532 as eb, 689 as vc from dual union all
select 'Apr-12' as monyar, 109 as cnt, 1256 as eb, 165 as vc from dual
)
select 'Month' as lbl, listagg(monyar,' | ') within group (order by monyar) as list from tbl
union all
select 'Count' as lbl, listagg(cnt,' | ') within group (order by monyar) as list from tbl
union all
select 'EB' as lbl, listagg(eb,' | ') within group (order by monyar) as list from tbl
union all
select 'VC' as lbl, listagg(vc,' | ') within group (order by monyar) as list from tbl

结果:

LBL     LIST
Month   Apr-11 | Apr-12 | Aug-11 | Dec-11 | Feb-12 | Jan-12 | Jul-11 | Jun-11 | Mar-12 | May-11 | Nov-11 | Oct-11 | Sep-11
Count   34 | 109 | 36 | 67 | 90 | 45 | 567 | 23 | 123 | 54 | 45 | 23 | 78
EB      1237 | 1256 | 10234 | 9823 | 3487 | 2398 | 10765 | 9652 | 7532 | 9834 | 11929 | 10923 | 8799
VC      428 | 165 | 1092 | 874 | 937 | 245 | 1278 | 235 | 689 | 87 | 346 | 359 | 987

使用管道作为分隔符,您可以将第二列拆分为任意多列。

LISTAGG 是一个 Oracle 函数,我不确定 sql server 中是否存在 1:1 等效项,因此如果必须在 sql server 中运行,您必须以一种或另一种方式模拟垂直连接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 2014-10-23
    • 2017-03-06
    • 2010-10-13
    • 1970-01-01
    相关资源
    最近更新 更多