Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
透视和取消透视SQL中具有多个列和行的表_Sql_Sql Server_Pivot_Pivot Table_Unpivot - Fatal编程技术网

透视和取消透视SQL中具有多个列和行的表

透视和取消透视SQL中具有多个列和行的表,sql,sql-server,pivot,pivot-table,unpivot,Sql,Sql Server,Pivot,Pivot Table,Unpivot,我需要输出下表如下。 下面是现有的数据库表输出 UNIQUE_ID PARTICULARS 18-Jan 18-Feb 18-Mar ----- ----- ----- ----- ----- 1 Direct Cost 3,393 3,776 3,776 1 Quarter

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

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转换了财务月,但无法将季度转换为列

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 
在上述查询中,缺少季度值


财务月季度可能与我的理解相同,并以同一时间为轴心。请在此方面提供帮助。

尝试的查询在
取消激活后缺少
数据透视
(条件聚合,例如
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)

以文本格式发布DDL和示例数据比图像更有用。因为图像不能被使用或复制。另外,请将您迄今为止在该问题上所做的尝试放在这里。您的基表设计看起来很奇怪。您是否确实有实际日期(月)的列名,或者这些数据是从外部资源导入的,您需要将其正常化。您的基表将来会发生什么情况?列名是否会更改为其他月份/日期?@peter这是从宏excel表提取到SQL表的。表中有更多的月份,不重复,我在这里作为示例月份。月份也与季度相关,以获取更多信息。我需要从数据集中获得如上所述的输出。@SurajKumar我已经编辑了你提到的文章