sql - 需要写子查询来计算每个月的收入

标签 sql oracle oracle11g

我的 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/

相关文章:

sql - 在 Oracle 11g 上授予创建 View

sql - 如何优化 View ?

java - 从 sqldeveloper 运行 sql 脚本与从 java 代码运行

mysql - 当前 MySQL IN 语句字符串 Cats 作为 "OR"- 需要它作为 "And"语句

c# - 一次访问数据库就可以在 LINQ 中更新?

java - 数据库交互后重定向

java - DdlTransactionIsolatorJtaImpl 无法找到 TransactionManager

SQL Server 2005 休眠 SPID 阻塞另一个 SPID

java - SQL 连接在普通 Java 中有效,但在 Android 中无效

java - 将非英文字符串插入UTF-8 Oracle数据库