Business Week logic in Oracle SQL - sql

I am using the following Oracle SQL query to work out if a week is more than 6 business weeks ago.
,CASE
WHEN to_char(next_day(CURRENT_TIMESTAMP,'sunday'),'iw')-6 <= to_char(next_day(m.COMPLETED_DT,'sunday'),'iw') THEN 'Yes'
ELSE 'No' END AS "Completed.Prior 6 Weeks"
The trouble is that this statement does not take into account the year, so now it is 2015, the business week is 1 and everything in the previous year has a greater business week.
I some how need to take into account the year as well, something like the below kind of works, but doesn't take into consideration the Business Week.
CURRENT_TIMESTAMP -42 >= m.COMPLETED_DT
Any help greatly appreciated

Rather than using TO_CHAR() to get the business week (which I think means the week starting on Monday), you should use TRUNC() - this way the year is preserved:
, CASE WHEN TRUNC(NEXT_DAY(SYSDATE, 'sunday'), 'iw') - 42 >=
TRUNC(NEXT_DAY(m.COMPLETED_DT, 'sunday'), 'iw') THEN 'Yes'
ELSE 'No' END AS "Completed.Prior 6 Weeks"
Note that I used 42 instead of 6 as Oracle date arithmetic uses days.
I'm not sure I see an advantage to using CURRENT_TIMESTAMP over SYSDATE. If you want to use the ANSI standard, you would want CURRENT_DATE ... I don't think you need the granularity of CURRENT_TIMESTAMP here, especially since you're truncating it. Also, you had <= when I think you mean >= (from what you have later in the question).

Related

Trying to accommodate relative defined date, such as 5 days ago, into my fixed date condition in PostgreSQL

I'm trying to condition my WHERE clause to accommodate relatively defined dates into my date filter. I'm pretty confused what type I need to use, if it's CONVERT or TO_DATE function, or if I need to put a CASE WHEN statement into my code.
This is the code that I have written so far:
WHERE event_create_verified_user_success.created_at_utc_date
BETWEEN DATE '2021-11-29' AND DATE '2021-12-05'
And this is the condition of the activity I need to finish:
If the desired date-period is not set manually using fixed dates like from “2021-11-29”
to “2021-12-05”, how would you change the where-clause to consider all data from relative
defined dates: “consider messages created between 10 days and 5 days ago (inclusive)”
I've only started PostgreSQL yesterday and the last time I've handle SQL was probably 4 years ago so I'm pretty confused at how much SQL has changed since then.
Thank you so much for helping!
The basic syntax hasn't really changed in the last 4 years (or even 15 years).
You can use current_date to obtain "today's date". You can subtract days from that
where ... between current_date - 10 and current_date - 5
If created_at_utc_date is a timestamp (= date and time) rather than a date (=date without time) it's better to use a range query though:
where created_at_utc_date >= current_date - 10
and created_at_utc_date < current_date - 4
Note the < combined with the next day you want to compare with.

How can I get the same week day from previous year in Tableau using a calculated field? For example the week day of 06/02/2020

I need to add the week day from previous year on the 3rd column.
Form example 06/02/2021 was on a Wednesday. I need the week day of 06/02/2021.
Thanks
try this:
SELECT DATENAME(weekday,DATEADD(YEAR, -1, '06-02-2021'))
Tableau is notoriously difficult to figure out work days. As some have suggested, it may be easier to use the underlying database to calculate this. However, if you need to do this in Tableau it can be accomplished like this:
DATEADD(
"day"
,CASE LEFT(LOWER(DATENAME("weekday",[DateFieldUsed])), 3)
WHEN 'fri' THEN 3
WHEN 'sat' THEN 2
ELSE 1
END
,[DateFieldUsed]
)
Basically, if it is Friday you need to add 3 days to the date, Saturday you need to add, and in any other situations just add 1 date. (please test, not doing this in Tableau so the numbers may be off).
I may have misunderstood the question, the example isn't exactly what you're asking. If your current value is 6/2/2021, and you are trying to find the week day of 6/2/2020, I would do this:
DATENAME("weekday", DATEADD(year, -1, [DateFieldUsed])

How to take aggregate of a data Grouped by Financial Year in SQL

My Financial Year is between 1st July to 30th June e.g 2018-07-01 and 2019-06-30
I have following data.
i want to aggregate of Rate column by financial year in sql
please help.
You can get the financial year by either subtracting or adding six months.
In ANSI/ISO standard SQL, this looks like:
select extract(year from fromDate + interval '6 month') as fiscal_year
sum(rate)
from t
group by extract(year from fromDate + interval '6 month');
This uses standard SQL -- which ironically does not work in most databases. Date functions are notoriously database-specific, so the exact code might differ in your database (which is unspecified as I write this).
If I correctly understood what you're looking for, this should work for you.
SELECT
SUM(Rate) as Rate,
CASE WHEN MONTH(FromDate) < 7 THEN YEAR(FromDate) - 1 ELSE YEAR(FromDate) END AS Fiscal_Year
FROM #t
GROUP BY CASE WHEN MONTH(FromDate) < 7 THEN YEAR(FromDate) - 1 ELSE YEAR(FromDate) END

SQL searching in 2 week interval from the current date

I'm having trouble setting up a SQL to bring up all information 2 weeks before and after the current date. Here is what I am currently doing:
Select WRK.Wrk, WRK.Client, WRK.Status, WRK.TAT, WRK.Due
From WRK
WHERE WRK.Due >= now()
Order By WRK.Due Desc, WRK.Status Desc
This gets me everything due on or after the current date but when I try to add lines to indicate 2 weeks before and after the current date I get errors.
Thanks
Date/time function vary significantly among databases. The use of now() makes me think of MySQL. The syntax in MySQL is:
where wrk.Due between date_sub(curdate(), interval 2 weeks) and date_add(curdate, interval 2 weeks)
Note that between includes the end dates, so this might be off by a day in either direction.
You can implement similar logic in other databases, but the specific functions would look different.

SQL Query data range limit

I have this where SQL Query. I want to know if I can limit that user can only pick 2 days of of date range. meaning they cannot pick 1 month or 1 week or more than 2 days difference of querty data range.
WHERE
To_Date(to_char(B.time_stamp, 'DD-MON-YYYY')) >= To_Date('?DATE1::?','MM/DD/YYYY')
and To_Date(to_char(B.rest_date, 'DD-MON-YYYY')) <= To_Date('?DATE2::?','MM/DD/YYYY')
If I understand your question, you are just asking how to limit the two dates so that they are within 2 days of each other. Date math is pretty simple in Oracle (which is my guess as to the DB you are using):
WHERE ABS(Date1 - Date2) <= 2
You don't need to convert it at all with to_char or anything else, since it is stored internally as an actual date. You can use this same type of logic to make sure it is less than 16 hours:
WHERE ABS(Date1 - Date2) <= 16/24
As long as you remember to adjust your units appropriately.
Note that in this case, 2 days means 48 hours. If you mean it has to be 2 actual days, then it is slightly different.