Solved

Last 12 months of data

Posted on 2016-09-16
2
27 Views
Last Modified: 2016-09-19
Hi All,

I am using the following code in my query:

       WHERE prt.tr_date BETWEEN TRUNC (ADD_MONTHS (SYSDATE, -13), 'MM')  
       AND TRUNC (LAST_DAY (ADD_MONTHS (SYSDATE, -1))+1)

It works. But when I run the report today (9/16) and look at the dates its pulling, its starting from 8/22/15 to 8/26/16.
Shouldn't the code be pulling from 9/5/15 (first September date) to today 9/16/16?
0
Comment
Question by:metalteck
  • 2
2 Comments
 
LVL 48

Expert Comment

by:PortletPaul
Comment Utility
You are deducting 13 months from sysdate, that why it goes back 1 month more than expected.
0
 
LVL 48

Accepted Solution

by:
PortletPaul earned 500 total points
Comment Utility
Perhaps this will help you choose what you need:
select a, b, c, gt_date, lt_date, lt_date - gt_date, gt_mnth, lt_mnth, lt_mnth - gt_mnth
from (
select
     sysdate a
   , trunc(sysdate) b
   , trunc(sysdate, 'mm') c
   , add_months(trunc(sysdate),-12) gt_date
   , add_months(trunc(sysdate),0)  lt_date
   , add_months(trunc(sysdate, 'mm'),-12) gt_mnth
   , add_months(trunc(sysdate, 'mm'),0)  lt_mnth
from dual
)
;

Open in new window

I would NOT USE BETWEEN, instead I would use >= with < like this:

       WHERE ( prt.tr_date >= add_months(trunc(sysdate, 'mm'),-12)
            AND   prt.tr_date < trunc(sysdate)
                     )

i.e. if sysdate is sometime in Sep 17 2016, then that would equate to

       WHERE prt.tr_date >= 2015-09-17
       AND prt.tr_date < 2016-09-17 -- LESS THAN today

(which is exactly 1 year, and happens to be 366 days)

For more on between in date ranges please see: "Beware of Between"
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
T-SQL: Why is Syntax So Difficult 5 33
query Syntax 4 32
Oracle Subquery bad Join 11 41
Getting certain data from a string 1 22
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now