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.
Related
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
I have a DB which collects Work Hours Reports from workers,
and i've been asked to create an alert for a specific division
of active contracts which the last report date was bigger than 90 days
I have the ReportDate column which contains the entries and I am not sure how to create it distinct, without using the GETDATE function
as mentioned, I tried the getdate function which obviously is not valid in this case
(not code but the query needed)
select distinct (newcontractid)
where last ReportDate is bigger than 90 days...
You seem to want something like this:
select contractid
from t
where t.status = 'active' -- whatever this is
group by contractid
having max(reportdate) < current_date - interval '90 day';
This uses standard SQL syntax. In MySQL, the date functions would be:
having max(reportdate) < current_date - interval 90 day;
In SQL Server:
having max(reportdate) < dateadd(day, -90, getdate())
I want to write a SQL query in Oracle database for:
A priceindex(field name) have around 120(say) records each day and I have to display the priceindex name and today's date, if the avg of last 7 days record count is greater than Todays record count for the priceindex(group by priceindex).
Basically, There will be 56 priceindex and each should have around 120 records each day and is dump to database each day from external site. So want to make sure all records are downloaded to the database everyday.
Except for the clarification I requested in a Comment to your question (having to do with "how can we know today's final count, when today is not over yet), the problem can be solved along the following lines. Not tested since you didn't provide sample data.
From your table, select only the rows where the relevant DATE is between "today" - 7 and "today" (so there are really EIGHT days: the seven days preceding today, and today). Then group by PRICEINDEX. Count total rows for each group, and count rows just for "today". The rows for "today" should be less than 1/8 times the total count (this is easy algebra: this is equivalent to being less than 1/7 times the count of OTHER days).
Such conditions, at the group level, must be in the HAVING clause.
select priceindex
from your_table
where datefield >= trunc(sysdate) - 7 and datefield < trunc(sysdate) + 1
group by priceindex
having count(case when datefield >= trunc(sysdate) then 1 end) < 1/8 * count(*)
;
EDIT The OP clarified that the query runs every day at midnight; this means that "today" should actually mean "yesterday" (the day that just ended). In Oracle, and probably in all of computing, midnight belongs to the day that BEGINS at midnight, not the one that ends at midnight. The time-of-day at midnight is 00:00:00 (beginning of the new day), not 24:00:00.
So, the query above will have to be changed slightly:
select priceindex
from your_table
where datefield >= trunc(sysdate) - 8 and datefield < trunc(sysdate)
group by priceindex
having count(case when datefield >= trunc(sysdate) - 1 then 1 end)
< 1/8 * count(*)
;
I need to get the number of records present in my [RecordsTable] for the last 3 months.
However the catch is I need the records which are processed between 10PM and 2AM.
For example --
07/01/2015 10PM -- 07/02/2015 2AM
07/02/2015 10PM -- 07/03/2015 2AM
07/03/2015 10PM -- 07/04/2015 2AM
The below SQL gives me the records present on any particular day starting from May,2015.
But I am not able to get the timing(10PM-2AM of next day) embedded in the SQL and need some help.
SELECT CONVERT(VARCHAR(12), RecordDate, 101),count(RecordID)
FROM [RecordsTable](NOLOCK)
WHERE RecordDate > '2015-05-01'
GROUP BY CONVERT(VARCHAR(12), RecordDate, 101)
MSSQL Supports both Date and Time datatypes. You can break up your where statement to reflect both date and time conditions separately.
SELECT COUNT(Records)
FROM TABLE
WHERE CONVERT(Date,DateCol) BETWEEN 'MM/DD/YYYY' AND 'MM/DD/YYYY'
AND CONVERT(Time,DateCol) BETWEEN 'HH:MM:SS' AND 'HH:MM:SS'
Try the following:
SELECT count(1)
FROM RecordsTable
WHERE RecordDate > '2015-05-01'
AND NOT DATEPART(hour, RecordDate) BETWEEN 2 AND 21
I assume RecordDate is a datetime or datetime2 column. between 2 and 21 will return rows where the hour for RecordDate is between 2am and 9pm, inclusive. NOT between 2 and 21 will return the reverse, giving you data for 10pm, 11pm, 12pm, and 1am. This does not include any time between 2:00am and 2:59am. If you need to include events that occurred precisely at but not after 2:00am, things get a bit tricker, but similar code based on not between would apply.
To get records in the last 3 months you can use two ways -- one by month looks like this
WHERE MONTH(colname) >= MONTH(GETDATE()) -3
This will get you inclusive months but not partial months. To get partial months is a bit more tricky because you could mean (for example for today) the 9th day of 3 months ago or you could mean 90 days ago. In the first case this works
WHERE colname >= dateadd(month,-3, getdate())
and for 90 days ago
WHERE colname >= dateadd(day,-90, getdate())
To get between 10PM and 2AM use this
WHERE datepart(hour,colname) >= 22 OR datepart(hour,colname) <= 2
Use DATEPART
SELECT COUNT(1)
FROM Table1
WHERE RecordDate > '2015-05-01'
AND (DATEPART(HOUR, RecordDate) <= 2 OR DATEPART(HOUR, RecordDate) >= 22)
Try this
SELECT count(*) FROM tablename where created_at>='2015-03-17 07:15:10' and created_at<='2015-07-09 02:23:50';
You can even use between
SELECT count(*) FROM tablename where created_at between '2015-03-17 07:15:10' and '2015-07-09 02:26:50';
You can use curdate() to get today's date
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