我的 table 是这样的
summa_som date_operation
----- --------------
100 11/03/2005
500 13/07/2008
900 12/11/2015
预期结果
我想计算每个月的收入应该有 3 列: 收入、月份和年份:
Income Month Year
------ -------- ----
10000 February 2015
15000 December 2015
我试过这个但是,我不太明白子查询是如何工作的。这段代码应该让我明白我想要什么:
select max(summa_som * 0.01) income
from t_operation
where to_char(date_operation,'MM') = 11
and to_char(date_operation,'YYYY') = 2015
( select max(summa_som * 0.01) income_for_dec2015 from t_operation
where to_char(date_operation,'MM') = 12
and to_char(date_operation,'YYYY') = 2015 )
最佳答案
您可以使用过滤器和聚合来实现这一点:
select to_char(date_operation, 'Month') as mn,
to_char(date_operation, 'YYYY') as yr,
max(summa_som) * 0.01 as income
from t_operation
where date_operation >= date '2015-11-01'
and date_operation < date '2016-01-01'
group by to_char(date_operation, 'Month'),
to_char(date_operation, 'YYYY')
如果您想要数据存在的所有月份和年份的结果,您可以删除过滤条件。
关于sql - 需要写子查询来计算每个月的收入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42777922/