SQLite query question - sql

I have 1 table:
table tb1 (
_id integer primary key autoincrement
,busnum text not null
, servdate date not null
);
I need a query which will get me all entries which have a "servdate" for the current week (the week starts on monday.)
So for example:
if I run the query on wednesday the 24th novemeber, it will get me all entries for monday 22nd, tuesday 23rd and wednesday 24th.
If I ran the query on sunday 28th, it will get me all entries for the full week (mon - sun), starting monday 22nd - sunday 28th.
If I run the query on monday, then it will just get me all entries for that day.
Thanks in advance. (thanks to admin for formatting my question)

Use the modifier weekday 1 (from the date time functions documentation) :
WHERE servdate BETWEEN date('now', 'Weekday 1', '-7 days') AND date('now')

Related

Difference of timestamp event rows using WHERE clause

I have two event tables with timestamped data: Registered, Signed_In.
Both have rows such as: original_timestamp, user_id
I am trying to find out users who haven't signed in within 30 days after registering. I have used the following query but I cannot add a WHERE clause to it.
I tried a query but I am getting hourly difference, whereas I wanted days difference which is unsupported in BigQuery.
SELECT Signed_In.user_id, TIMESTAMP_DIFF(Registered.original_timestamp, Signed_In.original_timestamp, HOUR) AS days_difference
FROM `test_db.Signed_In` signed_in
JOIN `test_db.Registered` registered
ON Signed_In.user_id = Registered.user_id
GROUP BY 1,2
ORDER BY 2 DESC
WHERE days_difference > '30'
I am getting two columns: user_id, days_difference but the days_difference shows hours and my WHERE clause is rejected when I use it.
You can try this below code-
Note: Using Ordinal Position for GROUP BY and ORDER BY is not a good practice. Its always safe and standard to use the column names directly.
SELECT Signed_In.user_id,
TIMESTAMP_DIFF(Registered.original_timestamp, Signed_In.original_timestamp, HOUR) AS days_difference
FROM `test_db.Signed_In` signed_in
JOIN `test_db.Registered` registered
ON Signed_In.user_id = Registered.user_id
WHERE DATE_DIFF(Registered.original_timestamp, Signed_In.original_timestamp, Day) > '30'
GROUP BY 1,2
ORDER BY 2 DESC
Just replace HOUR to DAY in your query:
SELECT Signed_In.user_id, TIMESTAMP_DIFF(Registered.original_timestamp, Signed_In.original_timestamp, DAY) AS days_difference
Correct values are:
MICROSECOND
MILLISECOND
SECOND
MINUTE
HOUR
DAYOFWEEK
DAY
DAYOFYEAR
WEEK: Returns the week number of the date in the range [0, 53]. Weeks begin with Sunday, and dates prior to the first Sunday of the year are in week 0.
WEEK(<WEEKDAY>): Returns the week number of timestamp_expression in the range [0, 53]. Weeks begin on WEEKDAY. datetimes prior to the first WEEKDAY of the year are in week 0. Valid values for WEEKDAY are SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.
ISOWEEK: Returns the ISO 8601 week number of the datetime_expression. ISOWEEKs begin on Monday. Return values are in the range [1, 53]. The first ISOWEEK of each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year.
MONTH
QUARTER
YEAR
ISOYEAR: Returns the ISO 8601 week-numbering year, which is the Gregorian calendar year containing the Thursday of the week to which date_expression belongs.
DATE
DATETIME
TIME

How to query last Saturday and the one before in SQL?

I would like to query data, which is between some week days, as follows:
Last Saturday
The Sunday before it
The Saturday before that queried Sunday on 2.
The Sunday before that queried Saturday on 3.
The query would run automatically every Monday, so I would like to set a dynamic condition for it, so that it automatically picks that days, without depending on any day it will run on in the future.
So for example if today is Monday 01/08/2018:
would be 01/06/2018
would be 12/31/2017
would be 12/30/2017
would be 12/24/2017
I would like to set that conditions in the WHERE clause. For now I am querying it this way, with constant dates for example for last week data:
SELECT *
FROM thisTable
WHERE Date(operation_date) BETWEEN '2018-06-10' AND '2018-06-16'
DBMS: Amazon Redshift
The DATE_TRUNC Function is your friend. It truncates to a Monday.
SELECT
CURRENT_DATE AS today,
DATE_TRUNC('week', CURRENT_DATE) as most_recent_monday,
DATE_TRUNC('week', CURRENT_DATE) - 2 AS most_recent_saturday,
DATE_TRUNC('week', CURRENT_DATE) - 8 AS sunday_before_most_recent_saturday
Returns:
2018-06-21 | 2018-06-18 00:00:00 | 2018-06-16 00:00:00 | 2018-06-10 00:00:00
Note that it treats the date as midnight at the beginning of the day. So, you don't really want to query Sunday to Saturday. You actually want to query Sunday to Sunday (which really means midnight at the start of Sunday to midnight at the start of the next Sunday). This assumes your source date is a timestamp.
If your source date is purely a date, then you would want to use Sunday to Saturday.
If you want to query everything from "last week" (if your definition is Sunday to Sunday), and assuming a timestamp, use:
SELECT *
FROM thisTable
WHERE operation_date BETWEEN
-- Most recent Monday minus 8 days = Two Sundays ago
DATE_TRUNC('week', CURRENT_DATE) - 8 AND
-- Most recent Monday minus 1 day = Most recent Sunday
DATE_TRUNC('week', CURRENT_DATE) - 1
(Well, unless you're on a Sunday already, but that's your problem!)
If the date is a date, you'd have to adjust it a bit.
last Sat: date_trunc('week',getdate())-interval '2 day'
prev Sat: date_trunc('week',getdate())-interval '9 day'
this is for Monday-based week

How to format date column to get the day name in Oracle SQL?

I have a oracle db and there are two columns so i want to display day ex:(SUNDAY, MONDAY...) according to given date in db.
Table Name: TBL_HOLIDAY_MASTER:
Holiday_date Description
***********************************
22-NOV-15 Weekly Holiday
23-NOV-15 Working Day
24-NOV-15 Working Day
29-NOV-15 Weekly Holiday
30-NOV-15 Working Day
21-MAY-17 Weekly Holiday
18-AUG-19 Weekly Holiday
I want output Like:-
Holiday_date Description
*************************************
SUNDAY Weekly Holiday
MONDAY Working Day
TUESDAY Working Day
SUNDAY Weekly Holiday
MONDAY Working Day
SUNDAY Weekly Holiday
SUNDAY Weekly Holiday
You may achieve this with TO_CHAR function with DAY parameter, in your case it would be:
SELECT TO_CHAR(Holiday_date,'DAY') as Holiday_date, Description
FROM TBL_HOLIDAY_MASTER;
You need to use TO_CHAR and FMDAY format to get the day name. FM is required to remove the trailing blank spaces.
TO_CHAR(date_column, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH')
For example,
SQL> SELECT TO_CHAR(SYSDATE + LEVEL -1, 'FMDAY', 'NLS_DATE_LANGUAGE=ENGLISH') "DAYS"
2 FROM DUAL
3 CONNECT BY level <= 7;
DAYS
---------
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
7 rows selected.

If actual date is Friday, also show rows at saturday and sunday Oracle DB

Normally I only show a record, if the actual date is one year later than the date in the database. How can I check if that day is a friday and then show also the records with the date of the saturday or sunday?
For example: Friday the 13th before one year. I will also show records from 14th(saturday) and 15th(sunday)
where myDate.arrival < TRUNC (SYSDATE) -365)
That's my actual statement.
You need a condition in case. Check the example below for reference:
WHERE txnday in
CASE to_char(sysdate, 'Day')
WHEN 'Friday' THEN // Include condition for Sat and Sun as well
ELSE // Include condition for only that day
END
Where Mydate.Arrival < Trunc (Sysdate) - 365
Or (to_char(Mydate.Arrival, 'D') = 6 AND Mydate.Arrival < Trunc (Sysdate) -363)
This way, you can check if that day is a Friday (6th day of week) and then select records whose Arrival value is less than sysdate - 363 (2 more days - Saturday and Sunday - included).

Pulling a dynamic day range from the previous year in DB2

I have two current SQL queries that I currently use to compare GM% from previous year vs. GM% this year. This is a daily report that I run every morning. The date arithmetic is not very solid and I am trying to find an alternative. Previously I thought that the report would only be for Monday forward, and not including the current day (ie if ran on Tuesday, it would only pull Monday. If ran on Monday it would not pull anything.) Recently that has changed to where when the report is ran on Monday, they want to see Friday-Sunday. What I am considering is setting it to pull the previous 5 day, not including the current day. (Ran on Monday would pull Thur, Fri, Sat, Sun.) The problem is that it has to be a day this year vs same day last year comparison. Anyone who;s tried this knows that it is not easy to get this. Here is my current code for the date arithmetic. I am at a loss guys, I could use some help.
Where DB1.TB1.CLM1>=Current Date-364 days - (DAYOFWEEK(CURRENT DATE) - 2) DAYS
And DB1.TB1.CLM1< Current Date- 364 days
If I hear you right, on Tuesday you would pull stats for Monday. Wed, you pull stats for Mon-Tues. Friday, you pull Mon-Thurs. And for all of these, you need the equivalent day prior year.
The trick is that now on Monday, you need to pull the previous weekend, i.e. Thu-Sun.
You have not defined what to do on Sunday, so I'm leaving that case out.
Try this WHERE statement:
where
( -- do this after Monday
dayofweek(current date) > 2 and
DB1.TB1.CLM1 between ((current date - 364 days) - (dayofweek(current date) - 2) days) and (current_date - 365 days)
)
or
( -- do this on Monday
dayofweek(current date) = 2 and
DB1.TB1.CLM1 between (current date - 368 days) and (current date - 365 days)
)