[英]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
中提供了ID
, month
, year
和total amt
字段。 incr/decr in total amt in previous period
的incr/decr in total amt in previous period
是total 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 period
的total amt in previous period
我无法Between Date( ) And DateAdd("M", -1, Date( ));
因为我没有约会,只有年份和月份。
如何将两列与year
和month
列进行比较,以及如何使用子查询获得最后两个派生列?
为了比较月份,有一个特定功能(适用于MS SQL和ORACLE)。 也有几天和几年的功能。 请参阅以下链接:
在论坛中已经讨论过的这个主题中可以找到有关此示例以及从子查询中获取派生列的示例:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.