簡體   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