繁体   English   中英

比较两个日期列并使用Oracle SQL添加派生列

[英]Comparing two date columns and adding derived column using Oracle SQL

我的表结构是这样的:

ID,
country,
month,
year,
total amt in previous period,
total amt during period,
incr/decr in total amt in previous period,
incr/decr in total amt during (month, year)

abc中提供了IDmonthyeartotal amt字段。 incr/decr in total amt in previous periodincr/decr in total amt in previous periodtotal amt in previous period total amt during period列中的total amt during period之差。

我写了这个查询:

select m.id, m.month, m.year, m.total_amt 
from abc m    
order by year, month desc;

对于total amt in previous periodtotal amt in previous period我无法Between Date( ) And DateAdd("M", -1, Date( )); 因为我没有约会,只有年份和月份。

如何将两列与yearmonth列进行比较,以及如何使用子查询获得最后两个派生列?

为了比较月份,有一个特定功能(适用于MS SQL和ORACLE)。 也有几天和几年的功能。 请参阅以下链接:

在论坛中已经讨论过的这个主题中可以找到有关此示例以及从子查询中获取派生列的示例:

Stackoverflow主题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM