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

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

Related

Return the number of months between now and datetime value SQL

I apologize, I am new at SQL. I am using BigQuery. I have a field called "last_engaged_date", this field is a datetime value (2021-12-12 00:00:00 UTC). I am trying to perform a count on the number of records that were "engaged" 12 months ago, 18 months ago, and 24 months ago based on this field. At first, to make it simple for myself, I was just trying to get a count of the number of records per year, something like:
Select count(id), year(last_engaged_date) as last_engaged_year
from xyz
group by last_engaged_year
order by last_engaged_year asc
I know that there are a lot of things wrong with this query but primarily, BQ says that "Year" is not a valid function? Either way, What I really need is something like:
Date() - last_engaged_date = int(# of months)
count if <= 12 months as "12_months_count" (# of records where now - last engaged date is less than or equal to 12 months)
count if <= 18 months as "18_months_count"
count if <= 24 months as "24_months_count"
So that I have a count of how many records for each last_engaged_date period there are.
I hope this makes sense. Thank you so much for any ideas
[How to] Return the number of months between now and datetime value [in BigQuery] SQL
The simples way is just to use DATE_DIFF function as in below example
date_diff(current_date(), date(last_engaged_date), month)

Oracle SQL: Count Weekdays of a Calendar Week

So I want to make a query to show me if a certain calendar week has all 7 Day.
It would be okay if it just returns the numbers 1-7.
The table that I have contains articles of the 3 month of 2020 but even so the first week just contains Wednesday to Sunday it still counts it as a calendar week.
With that select I would make pl/sql Script to check it and if yes something happens.
This is an example of the Table:
Date Articel_Id
14.10.2020 78
15.10.2020 80
16.10.2020 96
17.10.2020 100
18.10.2020 99
Can I Use to_char() to check if Calendar Week has all 7 Days ?
If yes, how ?
The challenging is actually defining the weeks. If you want to define them using the ISO standard, then aggregate:
select to_char(date, 'IYYYY-IW') as yyyyww,
count(distinct trunc(date)) as num_days
from t
group by to_char(date, 'IYYYY-IW')
order by yyyyww;
This counts the number of days per week. I'm not sure if you want to filter, have a flag, or what the result set should look like. For filtering, using a having clause, such as having count(distinct trunc(date)) = 7.

How to fetch records from DB2 for the last 7 days (1 WEEK) from today

I am using DB2. I want to fetch records for this week from a column. I want to use similar to below (This is for SQL 2005)
Select count(*) from Applications WHERE ReceivedDate is this week. (Last 7 days including today)
Try a WHERE clause like this:
WHERE ReceivedDate > current date - 7 days

Oracle SQL: Dynamic timeframe calculation

Greetings all knowing Stack.
I am in a bit of a pickle, and I am hoping for some friendly assistance form the hive mind.
I need to write a query that returns the difference in days between a registration date (stored in a table column) and the first day of the last September.
For example; assuming the query was being run today (24-10-2016) for a record with a registration date of 14-07-2010, I would want the script to return the difference in days between 14-07-2010 and 01-09-2016
However had I run the same query before the end of last August, for example on 12-08-2016, I would want the script to return the difference in days between 14-07-2010 and 01-09-2015.
I'm fine with the process of calculating differences between dates, it's just the process of getting the query to return the 'first day of the last September' into the calculation that is tripping me up!
Any input provided would be much appreciated.
Thankyou =)
Try this approach:
add four months to the current date
truncate this date to the first of year
subtract four months again
Add_Months(Trunc(Add_Months(SYSDATE, 4), 'year'), -4)
Hope this might help.
WITH T AS (SELECT TO_DATE('14-07-2010','DD-MM-YYYY') REG_DATE,
SYSDATE EXEC_DATE
FROM DUAL)
SELECT CASE WHEN TO_CHAR(EXEC_DATE,'MM') >= 9
THEN ADD_MONTHS(TRUNC(EXEC_DATE,'YEAR'),8)
ELSE ADD_MONTHS(TRUNC(ADD_MONTHS(EXEC_DATE,-12),'YEAR'),8)
END
- REG_DATE AS DIFF
FROM T;

Get the month and year now then count the number of rows that are older then 12 months in SQL/Classic ASP

I know this one is pretty easy but I've always had a nightmare when it comes to comparing dates in SQL please can someone help me out with this, thanks.
I need to get the month and year of now then compare it to a date stored in a DB.
Time Format in the DB:
2015-08-17 11:10:14.000
I need to compare the month and year with now and if its > 12 months old I will increment a count. I just need the number of rows where this argument is true.
I assume you have a datetime field.
You can use the DATEDIFF function, which takes the kind of "crossed boundaries", the start date and the end date.
Your boundary is the month because you are only interested in year and month, not days, so you can use the month macro.
Your start time is the value stored in the table's row.
Your end time is now. You can get system time selecting SYSDATETIME function.
So, assuming your table is called mtable and the datetime object is stored in its date field, you simply have to query:
SELECT COUNT(*) FROM mtable where DATEDIFF(month, mtable.date, (SELECT SYSDATETIME())) > 12