我需要将标准表中的表转换为更加汇总的版本。行应该基于2013年或2014年。我正在考虑使用枢轴,但我认为不必要。下面的代码失败是因为我刚刚创建的列的功能是否有办法解决这个问题?
CODE
CREATE VIEW -- select * from
[03_rdm].[AR_LedgerByInvoice]
as
select [EY_Business_Unit]
,(Case when [AR_Fiscal Year] = 2014 then 'AR_Outstanding_Amount (CY)' else 'AR_Outstanding_Amount (PY)' end) [Aging_Bucket]
, [AR_1-30 Days Past Due]
, [AR_31-60 Days Past Due]
, [AR_61-90 Days Past Due]
FROM [02_cdm].[0040_AR_InvoiceSummary_2013_2014]
GROUP BY [Aging_Bucket]
最终结果应为:
期望的输出
Business Unit Aged Bucket [AR_1-30 Days] [AR_31-60 Days] [AR_61-90 Days] Total
100 ` AR_Outstanding_Amount (CY) x xx xxx xxxx
100 AR_Outstanding_Amount (PY) x xx xxx xxxx
答案 0 :(得分:3)
您无法按照在同一级别创建的别名进行分组'但你应该能够按同一个表达式分组,但也许你只想按年分组? (如果是这样,GROUP BY [AR_Fiscal Year]
可能就是你真正想要的那样):
CREATE VIEW -- select * from
[03_rdm].[AR_LedgerByInvoice]
as
select [EY_Business_Unit]
,(Case when [AR_Fiscal Year] = 2014 then 'AR_Outstanding_Amount (CY)' else 'AR_Outstanding_Amount (PY)' end) [Aging_Bucket]
, [AR_1-30 Days Past Due]
, [AR_31-60 Days Past Due]
, [AR_61-90 Days Past Due]
FROM [02_cdm].[0040_AR_InvoiceSummary_2013_2014]
GROUP BY
[EY_Business_Unit],
CASE WHEN [AR_Fiscal Year] = 2014 THEN 'AR_Outstanding_Amount (CY)' ELSE 'AR_Outstanding_Amount (PY)' END
由于您没有提供任何样本数据或表格,我无法对其进行测试,但它应该有效。
或者您可能希望聚合行:
select [EY_Business_Unit]
,(Case when [AR_Fiscal Year] = 2014 then 'AR_Outstanding_Amount (CY)' else 'AR_Outstanding_Amount (PY)' end) [Aging_Bucket]
, SUM([AR_1-30 Days Past Due])
, SUM([AR_31-60 Days Past Due])
, SUM([AR_61-90 Days Past Due])
FROM [0040_AR_InvoiceSummary_2013_2014]
GROUP BY
[EY_Business_Unit]
,(Case when [AR_Fiscal Year] = 2014 then 'AR_Outstanding_Amount (CY)' else 'AR_Outstanding_Amount (PY)' end)
order by [EY_Business_Unit];