【问题标题】:Pivot and unpivot a table with multiple columns and Rows in SQL在 SQL 中透视和取消透视具有多列和多行的表
【发布时间】:2020-01-02 17:36:39
【问题描述】:

我需要在下表中输出如下。 下面是现有的数据库表输出。

UNIQUE_ID   PARTICULARS                   18-Jan    18-Feb    18-Mar
-----       -----                         -----     -----     -----
1           Direct Cost                   3,393     3,776     3,776
1           Quarter                       Q3 FY18   Q3 FY18   Q3 FY18
1           Revenue net Volume Discount   4,409     5,787     5,512
2           Direct Cost                   25,022    39,178    34,143
2           Quarter                       Q2 FY18   Q2 FY18   Q2 FY18
2           Revenue net Volume Discount   28,730    45,507    38,247

我需要将上面的表格转换为下面的输出。

UNIQUE_ID   FinancialMonth  Quarter     DirectCost  Revenue net Volume Discount
1           18-Jan          Q3 FY18     3,393       4,409
1           18-Feb          Q3 FY18     3,776       5,787
1           18-Mar          Q3 FY18     3,776       5,512
2           18-Jan          Q2 FY18     25,022      28,730
2           18-Feb          Q2 FY18     39,178      45,507
2           18-Mar          Q2 FY18     34,143      38,247

你能帮我把它转换一下吗?我已使用 unpivot 转换 FinancialMonth,但无法将 Quarter 转换为 Column。

SELECT UNIQUE_ID
       ,PARTICULARS
       ,[FinancialYearMonth] AS 'FinancialMonth'
       ,CASE WHEN PARTICULARS='Direct Cost'   
             THEN [FinancialValues] END AS [DirectCost]
       ,CASE WHEN PARTICULARS='Revenue net Volume Discount'   
             THEN [FinancialValues] END AS [RevenueNetVolumeDiscount]

FROM DBO.Raw_Monthly
UNPIVOT   
  ( 
        FinancialValues
    FOR [FinancialYearMonth] IN(
       Jan18
      ,[Feb18]
      ,[Mar18]


       ) 
   ) AS unpv 

在上述查询中,季度值缺失。

FinancialMonthQuarter 都可能与我的理解不同,并且与我的理解相同。你能帮忙吗?

【问题讨论】:

  • 以文本格式发布 DDL 和示例数据比图片更有帮助。由于图像不能使用或复制。还要把你迄今为止尝试过的问题放在这个问题上。
  • 您的基表设计看起来很奇怪。您是否真的有实际日期(月)的列名,或者这些数据是从您需要规范化的外部资源导入的。今后您的基表会发生什么 - 列名会更改为其他月份/日期吗?
  • @peter 这是从宏 excel 表中提取到 SQL 表的原样。有更多的月份驻留在表中,而不是重复的,我在这里放了样本月份。月份也与季度有关以获取更多信息。我需要从数据集中得到如上的输出。
  • @SurajKumar 我已经编辑了你提到的帖子

标签: sql sql-server pivot pivot-table unpivot


【解决方案1】:

尝试的查询在unpivoting 之后缺少pivoting条件聚合,例如包含通过分组聚合的case..when 子句)。因此,请考虑:

 SELECT [Unique_ID], [FinancialMonth], 
        MAX(CASE WHEN [Particulars]='Quarter' THEN [FinancialValues] END) AS [Quarter],
        MAX(CASE WHEN [Particulars]='Direct Cost' THEN [FinancialValues] END) AS [DirectCost],
        MAX(CASE WHEN [Particulars]='Revenue net Volume Discount' THEN [FinancialValues] 
        END) AS [Revenue net Volume Discount]
   FROM Raw_Monthly
UNPIVOT  
   (
    [FinancialValues] FOR [FinancialMonth] IN ( [18-Jan] ,[18-Feb] ,[18-Mar] ) 
   ) AS unpvt
  GROUP BY [Unique_ID], [FinancialMonth]
  ORDER BY [Unique_ID], 
           CONVERT(date, REVERSE(SUBSTRING(REVERSE([FinancialMonth]),1,3))+
                  ' 20'+  SUBSTRING(REPLACE([FinancialMonth],'-',''),1,2) , 13)

Demo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多