【问题标题】:tsql - how to pivot the following table?tsql - 如何旋转下表?
【发布时间】:2012-09-22 07:50:05
【问题描述】:

如何按第 1 周、第 2 周、第 3 周等旋转下表。每个月?谢谢。

例如 数据库表:

这是我需要的表:

这就是我所做的,但我需要更有效的方法来做到这一点。

SELECT    'Oct' AS [Month]
,[Ocd]
          ,(select Wk_Cmpl from tb1 where WkNum = '1' and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk1
          ,(select WKLY_PCT from tb1 where WkNum = '1' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk1%]
          ,(select WE from tb1 where WKNum = '2' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk2
          ,(select WKLY_PCT from tb1 where WkNum = '2' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk2%]
          ,(select WE from tb1 where WkNum = '3' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk3
          ,(select WKLY_PCT from tb1 where WkNum = '3' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk3%]
          ,(select WE from tb1 where WkNum = '4' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk4
          ,(select WKLY_PCT from tb1 where WkNum = '4' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk4%]
          ,(select WE from tb1 where WkNum = '5' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk5
          ,(select WKLY_PCT from tb1 where WkNum = '5' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk5%]
          ,[WKLY_AVG]                           As [Wk Avg] 
          ,[MTH]                                AS [Mo. Cmpl] 
          ,[COMB_FYTD_COMPLT_ALL]               As [M/YTD Total]
          ,[COMB_FYTD_COMPLT_TARGET_PCT]        As [% Goal]
FROM tb1

【问题讨论】:

  • 看起来您已经在 Excel 中完成了。您是在问如何在 SQL 中完全做到这一点?你用的是什么关系型数据库?
  • 是的,我正在尝试在 SQL、SSMS 2008 中执行此操作,谢谢。
  • 您查看过MS BOL 中的PIVOT SQL 命令吗?您已经在 SSMS / SQL 中尝试过什么?
  • @JamesL。请参阅上面的查询。

标签: sql sql-server tsql pivot


【解决方案1】:

为此,您需要使用 UNPIVOTPIVOT。您不清楚如何确定WklyAvg%Goal,但这应该可以帮助您开始:

select p1.mo,
    p1.[wkCmpl_1], p1.[wkCmplPct_1], p1.[wkCmpl_2], p1.[wkCmplPct_2],
    p1.[wkCmpl_3], p1.[wkCmplPct_3], p1.[wkCmpl_4], p1.[wkCmplPct_4],
    p1.[wkCmpl_5], p1.[wkCmplPct_5],
    t1.WkAvg,
    t1.MoCmpl,
    t2.M_YTD_Total,
    t1.PctGoal
from 
(
  select mo,
    [wkCmpl_1], [wkCmplPct_1], [wkCmpl_2], [wkCmplPct_2],
    [wkCmpl_3], [wkCmplPct_3], [wkCmpl_4], [wkCmplPct_4],
    [wkCmpl_5], [wkCmplPct_5]
  from 
  (
    select datepart(month, wk_endt) mo,
        value,
        col + '_' + cast(wkNum as varchar(10)) col
    from 
    (
      select wk_endt,
        wkNum,
        cast(wkCmpl as decimal(10, 2)) wkCmpl,
        wkCmplPct
      from yourtable
    ) x
    unpivot
    (
      value
      for col in (wkCmpl, wkCmplPct)
    ) u
  ) x1
  pivot
  (
    max(value)
    for col in ([wkCmpl_1], [wkCmplPct_1], [wkCmpl_2], [wkCmplPct_2],
               [wkCmpl_3], [wkCmplPct_3], [wkCmpl_4], [wkCmplPct_4],
               [wkCmpl_5], [wkCmplPct_5])
  ) p
) p1
inner join
(
  select month(wk_endt) mo,
    wkcmpl,
    avg(WkAvg) as WkAvg,
    MoCmpl,
    max(M_YTD_Total) M_YTD_Total,
    PctGoal
  from yourtable
  group by month(wk_endt), wkcmpl, MoCmpl, PctGoal
) t1
  on p1.mo = t1.mo
  and p1.wkCmpl_1 = t1.wkcmpl
inner join
(
    select month(wk_endt) mo, max(M_YTD_Total) M_YTD_Total, MAX(wknum) wknum
    from yourtable
    group by month(wk_endt)
) t2
    on t1.mo = t2.mo

SQL Fiddle with Demo

【讨论】:

    猜你喜欢
    • 2014-10-05
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-02
    • 2013-10-22
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    相关资源
    最近更新 更多