[英]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.