【发布时间】:2019-05-10 13:50:37
【问题描述】:
我正在使用单个仓库表,其中包含“2015 年费用”、“2015 年收入”、“2016 年费用”、“2016 年收入”等列。我需要将它们拆分为“收入”、“费用” ,和“开票年份”,以便做一些分析。许多记录都有多年的费用和收入。
我尝试的 CASE 语句仅在第一年中提取,但我需要它在所有年份中提取。
这是我的案例陈述:
(CASE when 2015_revenue IS NOT NULL then 2015_revenue
when 2016_revenue_$ IS NOT NULL then 2016_revenue
END) as revenue,
(CASE when 2015_fee IS NOT NULL then 2015_fee
when 2016_fee IS NOT NULL then 2016_fee
END) as fee,
(CASE when 2015_revenue IS NOT NULL then '2015'
when 2015_fee IS NOT NULL then '2015'
when 2016_revenue IS NOT NULL then '2016'
when 2016_fee IS NOT NULL then '2016'
end) as bill_year
有什么想法吗?
【问题讨论】:
-
您将不得不写出这个 sql,因为架构不好。 ://
-
您能否编辑您的问题,为输入数据提供一些示例行以及您希望从查询中获得的输出示例?然后我们将能够提供一些建议。
标签: sql amazon-redshift