简体   繁体   中英

Comparing two date columns and adding derived column using Oracle SQL

My table structure is this:

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)

The ID , month , year and total amt fields are available in table abc . The incr/decr in total amt in previous period is the difference between total amt in previous period and total amt during period columns.

I wrote this query:

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

For the total amt in previous period I could not use Between Date( ) And DateAdd("M", -1, Date( )); as I have no date but just year and month.

How to compare the two columns with the columns year and month and how to have the last two derived columns using subqueries?

For comparing Month there is an specific function (that works for MS SQL and ORACLE). There's one function for days and years too. See links below:

YEAR

MONTH

DAY

Examples of this and getting derived columns from subqueries can be found in this topic already discussed in the forum:

Stackoverflow topic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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