Proper record not displaying for data needed in oracle - sql

I have a requirement where I want to show the relevant data which should not be greater than 6 months from todays date. SO I wrote the below query for the same but it is showing me the data of 2019 also.
select CR.CHANGEREQUESTID ,CR.CHANGEREQUESTNUMBER, CR.STATENAME, CR.NETWORKTYPE, CR.CREATEDON,
CR.LASTMODIFIEDON,
NHQ.SAP_ID, NHQ.STATE, NHQ.NEW_LATITUDE, NHQ.NEW_LONGITUDE, NHQ.OLD_LATITUDE, NHQ.OLD_LONGITUDE
from CHANGEREQUESTS CR
inner join TBL_NHQ_CIRCLE_INFO NHQ
on CR.CHANGEREQUESTID = NHQ.CHNGREQUEST_ID
where CR.lastmodifiedon > add_months(sysdate, -12)
and CR.CHANGETYPEID=55;
Please suggest what is wrong here.

This condition gives you all record that are less than 1 year old:
where CR.lastmodifiedon > add_months(sysdate, -12)
If you wanted records older than 6 month, that would be:
where CR.lastmodifiedon < add_months(sysdate, -6)
On the other hand, if you want dates between 6 months ago and today:
where CR.lastmodifiedon > add_months(sysdate, -6) and CR.lastmodifiedon <= sysdate

Related

Get All records of previous date from current date in oracle

I want all the data from a table which is more than 6 months available in my table. So for that I wrote the below query but it wasn't giving the exact records.
Select * from changerequests where lastmodifiedon < sysdate - 180;
The issue is I was getting the records for 2nd april, 2020 which is not more than 6 months. Please suggest the query
If you want records that were last modified within the last 6 months, then you want the inequality condition the other way around:
where lastmodifiedon > sysdate - 180
Note that 180 days is not exactly 6 months. You might want to use add_months() for something more accurate:
where lastmodifiedon > add_months(sysdate, -12)

Difference in SQL Date Query with Parenthesis

I inherited an a multiple Machine Learning processes that use essentially the same date query except for parenthesis. The following 3 date queries give a different number of rows. What exactly is the difference between each date query to give a different amount of rows for each?
1)
WHERE
((dbo.FACTINVOICEHEADER.PAID_DATE >= '2019-02-01'
AND dbo.FACTINVOICEHEADER.PAID_DATE <= '2020-01-31')
OR (dbo.FACTINVOICEHEADER.PAID_DATE >= '2018-02-01'
AND dbo.FACTINVOICEHEADER.PAID_DATE <= '2019-01-31'))
2)
WHERE
((dbo.FACTINVOICEHEADER.PAID_DATE >= '2018-02-01'
AND dbo.FACTINVOICEHEADER.PAID_DATE <='2020-01-31'))
3)
WHERE
dbo.FACTINVOICEHEADER.PAID_DATE >= '2018-02-01'
AND dbo.FACTINVOICEHEADER.PAID_DATE <= '2020-01-31'
The first query is selecting 24 months, minus one day (Jan 31st, 2019).
The second query is selecting 24 months.
The third query is equivalent to the second one.

Oracle SQL for Previous Year Data

How can i write SQL which will fetch the data for current year -4
e.g. if i run this sql today so it should give me the data from 2014 to till date
if i run this sql in 2019 so it should give me the data from 2015 to till date
please assist.
You can make use of interval
SELECT *
FROM yourtable
WHERE yourcolumn >= trunc(sysdate, 'yyyy') - interval '4' year
AND yourcolumn < trunc(sysdate, 'yyyy');
This will produce results from 2015/Jan/01 to till date; If you need exactly 4 years worth of data then you need to tweak this SQL to include date and month along with the year.

Update a running sql script to update the last 2 years based on today's date

I currently have a simple sql script (in Oracle) that updates based a date range >='01JUN13'. I'm trying to modify this script to automatically update based on the last two years based on today's date. So the next month, I need the last two years of data greater than or equal to July 2013. Thanks.
It's not entirely clear from your question whether you want the date range to start from exactly two years ago, to the day , or from the start of the month.
Matching to the day:
select * from your_table
where date_col >= add_months(trunc(sysdate), -24)
/
Matching to the first day of the month:
select * from your_table
where date_col >= add_months(trunc(sysdate, 'MON'), -24)
/

Trying to Look back 4 whole months in teradata with ADD_MONTHS function in sql statement

I'm trying to go back and retrieve counts for the last 4 full months. This is an example of what I have so far:
SELECT datecolumn, Count(datacolumnA) AS CountOfdatacolumnA, datacolumnB
FROM tableA
WHERE datacolumnB='AA' AND datecolumn >= ADD_MONTHS(CURRENT_DATE, -4)
My results show the last four months plus the current month, October in this case. The problem is that June isn't showing the correct count for the entire month. I'm only getting a partial count for the month.
You need to adjust to the start of the month. You can do this by subtracting the day of the month to get the '0th' of the month and then adding 1 to get the first. (I think dates in teradata are decimals with the int part being number of days since an epoch)
Select
datecolumn,
Count(datacolumnA) As CountOfdatacolumnA,
datacolumnB
From
tableA
Where
datacolumnB='AA' And
datecolumn >=
add_months(current_date, -4)
- extract(day from add_months(current_date, -4)) + 1