Checking to make sure a column has been updated in the last 5 days SQL - sql

I'm looking for the best possible way to check to make sure a column of dates, in a SQL database, have been updated in the last 5 days.
For example: The column contains 100 dates, all ranging from 1/1/2017 to 4/10/2017. Today is 4/19/2017. I'm looking at all of these dates and checking to see if any of them have been within the last 5 days of today, if not throw an exception/error/anything. Mainly I'm checking to see if this column is being updated regularly.
Thank you.

You can run a query and return a value
select (case when count(*) > 0 then 'OKAY' else 'NO UPDATES !!!' end)
from t
where datecol >= current_timestamp - interval '5 day';
This will work in more updates.
But, actually, I would probably do the logic in the application:
select max(datecol) as max_datecol
from t;
Then do the logic in the application based on the recency of the last update.

You can count how many entries have been added in last 5 days.
SELECT COUNT(*) FROM table_name WHERE date_column > NOW() - 5 DAYS;
Then in the application just check:
if (result > 0) { there has been a new entry in last 5 days }
else {ther has not been a new entry in last 5 days}

Related

How to find the data of members in next 30 days

I am trying to write a logic to find a person membership is going to expiring in next 30days. Can, some one help me to write a query in hive?
I tried using:
select * from db where expiringdate > current_date;
But is showing all the records greater than current date. But I need only for next 30 days
You want two inequality conditions on the expiration date:
select *
from db
where expiringdate > current_date -- later than today
and expiringdate < add_months(current_date, 1) -- not later than one month from now
Or if you want exactly 30 days rather than one month, use date_add(current_date, 3).

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)

Fetch records with a where condition that specifies fetch records for this week in DB2

I am using DB2. I want to know the number of applications (Records/row) since the last seven days including today and my date column is ReceivedDate.
I basically want to write a query that intents to do the following: (DB2), please help me.
Select count(*) from Applications WHERE ReceivedDate is within 7 days
Try this query, which uses CURRENT DATE:
SELECT COUNT(*)
FROM Applications
WHERE ReceivedDate > CURRENT DATE - 7 DAY;
This answer assumes that a received date would never be in the future (since it can't have yet happened).
You can try below -
Select count(*) from Applications
WHERE ReceivedDate>CURRENT DATE - 7 Days and ReceivedDate<=CURRENT DATE

Sql query to return data if AVG(7 days record count) > (Today's record count)

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(*)
;

SELECT using timestamp in Oracle SQL

my problem is trying to use a SELECT statement and order the top 10 by a certain column. I managed to compile something after searching through lots of forums, however I need to confirm that the timestamp in one field is within the last week. I have gotten this to execute however i'm not sure whether this is correct as I can't print the value for the where clause:
SELECT itemid, count(itemid)
FROM Rateddate
WHERE TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)
GROUP BY itemid;
TLDR:
TO_CHAR(CURRENT_TIMESTAMP - DATE_RATED) < TO_CHAR(7)
does this make sure the date_rated timestamp is less than a week old?
It would make more sense to say
WHERE date_rated > sysdate - interval '7' day
if you want data from the last 168 hours. You may want
WHERE date_rated > trunc(sysdate) - interval '7' day
if you want data from any point in the day 7 days ago rather than caring about what time of day it is currently.
Wouldn't this work for you:
trunc(CURRENT_TIMESTAMP - DATE_RATED) < 7
This is assuming that DATE_RATED is a date field. If not, you need to convert it first with TO_DATE().