Change timestamp to select from previous week starting now - google-bigquery

I am running a query in Bigquery that selects all of the data entires made within the last week. However, I just noticed that the code I have is selecting the data from the previous week (Sunday to Sunday) and not the immediate week starting from whenever I run the query. How can I change this query to accomplish that. Thanks for the help!
#standardsql
SELECT count(distinct DeviceID)
FROM `dataworks-356fa.FirebaseArchive.test2`
Where PeripheralType = 5
AND EXTRACT(WEEK FROM createdAt) = EXTRACT(WEEK FROM CURRENT_TIMESTAMP()) - 1
AND serial != 'null'
I tried to run the query below but this would not select any data entries that were made today.
AND EXTRACT(day FROM createdAt) = EXTRACT(day FROM CURRENT_TIMESTAMP()) - 7

assuming that createdAt is a timestamp - you should use:
WHERE DATE(createdAt) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND CURRENT_DATE()
So, your example would be like below
#standardsql
SELECT count(distinct DeviceID)
FROM `dataworks-356fa.FirebaseArchive.test2`
Where PeripheralType = 5
AND DATE(createdAt) BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY) AND CURRENT_DATE()
AND serial != 'null'

Related

select 10 day old entries BigQuery

I need to get only entries that are 10 days + old in BigQuery, I went to other questions and google doc (which is confuse as hell) but could not find a the result I expected.
My query:
SELECT Id
FROM myTable
WHERE eventTS > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)
The this query is returning values that are not 10 days + old.
when I add -10 DAY doesn't return nothing.
Any help is MUCH welcome!
Thanks in advance.
Try following Bigquery functions SELECT CURRENT_TIMESTAMP() as Curr_Ts, TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY) as Sub_Ts , TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -10 DAY) as Add_Ts It will Add & Substract 10 days from CURRENT_TIMESTAMP(). Output will be as :-
Your Query will get modified as :-
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)
Or
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -10 DAY)
Try replacing > with <:
SELECT Id
FROM myTable
WHERE eventTS < TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 10 DAY)

MSSQL query for all records between two date range of current day

I might not be asking this right, but basically I need a query that when ran, returns all records entered from the 1st till the 15th of the current month. And when the 15 passes only return the records from the 16th till the end of the current month.
I've tried to build something like this but its for bigquery and not sql, and I can't seem to find something similar for mssql 2016.
select sample_id
from dbo.table
WHERE date_entered BETWEEN DATE_ADD(CURRENT_DATE(), -15, 'DAY') AND CURRENT_DATE()
or
WHERE date_entered BETWEEN CAST(eomonth(GETDATE()) AS datetime) AND CURRENT_DATE()
Regardless of the today's date, I need the 1st till today, until the 15th. Then the 16th till today, until the end of the month. Sorry I'm new to SQL.
UPDATE: I was able to solve this issue with the example provided by #GordonLinoff . Thank you Gordon!
SELECT rowguid, ModifiedDate
FROM [AdventureWorks2017].[Person].[Person]
WHERE Year(ModifiedDate) =Year(getdate()) and month(ModifiedDate) =month(getdate()) and
((day(getdate()) <= 15 and day(ModifiedDate) <=15))
Or
((day(getdate()) >= 16 and day(ModifiedDate) >=16))
The description of your logic is a bit hard to follow, but you seem to want something like this:
where date_entered >= datefromparts(year(getdate(), month(getdate(), 1)) and -- this month
(day(getdate()) <= 15 or
day(getdate()) > 15 and day(date_entered) > 15
)
This was MySQL:
SELECT *
FROM dbo.table
WHERE date BETWEEN CASE WHEN DAY(CURRENT_DATE) <= 15
THEN DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
ELSE DATE_FORMAT(CURRENT_DATE, '%Y-%m-16')
END
AND CASE WHEN DAY(CURRENT_DATE) <= 15
THEN DATE_FORMAT(CURRENT_DATE, '%Y-%m-15')
ELSE LAST_DAY(CURRENT_DATE)
END
Big Query:
SELECT *
FROM table
WHERE date BETWEEN CASE WHEN EXTRACT(DAY FROM CURRENT_DATE) <= 15
THEN DATE_TRUNC(CURRENT_DATE, MONTH)
ELSE DATE_ADD(DATE_TRUNC(CURRENT_DATE, MONTH), INTERVAL 15 DAY)
END
AND CASE WHEN EXTRACT(DAY FROM CURRENT_DATE) <= 15
THEN DATE_ADD(DATE_TRUNC(CURRENT_DATE, MONTH), INTERVAL 14 DAY)
ELSE DATE_ADD(CURRENT_DATE, INTERVAL 31 DAY)
END
This should give
date between 1 and 15
or date between 16 and last_of_the_month
I've tried to build something like this but its for bigquery
Whatever example you use - it is not working in BigQuery either!
Below is working example for BigQuery Standard SQL and uses some "tricks" to avoid using redundant code fragments
#standardSQL
SELECT sample_id
FROM `project.dataset.table`,
UNNEST([STRUCT(
EXTRACT(DAY FROM date_entered) AS day,
DATE_TRUNC(date_entered, MONTH) AS month
)])
WHERE DATE_TRUNC(CURRENT_DATE(), MONTH) = month
AND IF(
EXTRACT(DAY FROM CURRENT_DATE()) < 16,
day BETWEEN 1 AND 15,
day BETWEEN 16 AND 99
)

In Bigquery - How to query Yesterday data & Last 7 days data from Firebase event table

I use below query to fetch data for specified data range.
SELECT event_date, count(event_name) as APP_Installs FROM
`<Table>.events_*` WHERE _TABLE_SUFFIX BETWEEN '201900201' AND '20190228'
and event_name='first_open' group by 1
How to query event table for yesterday data without mentioned the
date value in Bigquery?
How to query event table for last 7 days data without mentioned the
date value in Bigquery?
Pls. Help
Date functions are what you need here.
To look at the last 7 days, use the following query:
SELECT event_date, count(event_name) as APP_Installs
FROM `<Table>.events_*`
WHERE _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)) AND FORMAT_DATE('%Y%m%d', CURRENT_DATE())
AND event_name = 'first_open'
GROUP BY 1
To look at yesterday's data, use the following query:
SELECT event_date, count(event_name) as APP_Installs
FROM `<Table>.events_*`
WHERE _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
AND event_name = 'first_open'
GROUP BY 1

How to fetch data from events_ table in bigquery for last 24 hours by using standardsql?

I m using this query and i want to fetch the data of last 24 hours from the events_ table...
Select
CAST(TIMESTAMP_ADD(TIMESTAMP_MICROS(event_timestamp), INTERVAL 330
MINUTE) AS date) AS event_date,
event_name,user.value.string_value as context_device_id,
(event.value.string_value) as id,
(event_param.value.string_value) as contentType
FROM ``,
UNNEST(user_properties) AS user,
UNNEST(event_params) as event,
UNNEST(event_params) as event_param
where user.key="email" and event.key="postID" and
event_param.key="article_type" and
CAST(TIMESTAMP_ADD(TIMESTAMP_MICROS(event_timestamp), INTERVAL 330
MINUTE)AS date) between DATE_SUB(current_date(), INTERVAL 1 DAY) and
DATE_SUB(current_date(),INTERVAL 0 DAY)
But I want whenever query will run gives the data of last 24 hours only
means if i running the query at event 5pm today then it should fetch the data from yesterday 5pm to today's 5pm?
You need to update your table reference to use a wildcard, which can include multiple days, and then add a filter to restrict the tables that it matches. For example, you would want something like:
...
FROM `events_*`,
UNNEST(user_properties) AS user,
UNNEST(event_params) as event,
UNNEST(event_params) as event_param
WHERE _TABLE_SUFFIX >=
FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)) AND
user.key="email" and
...
The filter on the _TABLE_SUFFIX pseudo-column restricts the scan to the tables for today and yesterday, and then the filter on the timestamp as in your original query further restricts to a 24 hour span.
In case sql server you can use this in where clause
where event_timestamp>=dateadd (hour , -24 , getdate()) and event_timestamp<getdate()
As an alternative, GETDATE() in MSSQL will let you do integer division:
SELECT GETDATE(), GETDATE() - 1
Result at this moment: 2018-08-31 07:38:18.260
2018-08-30 07:38:18.260
So in your case BETWEEN GETDATE() - 1 AND GETDATE() will do the trick as well.

BigQuery Where Date is Less Than or Equal to 3 Days Minus Current Date

I'm trying to create a query to only return data where date is minus 3 days from the current date. I've tried:
date <= DATE_ADD(CURRENT_DATE(), -3, 'DAY')
But this returns Error: Expected INTERVAL expression
See WHERE clause in below example
#standardSQL
WITH yourTable AS (
SELECT i, date
FROM UNNEST(GENERATE_DATE_ARRAY('2017-04-15', '2017-04-28')) AS date WITH OFFSET AS i
)
SELECT *
FROM yourTable
WHERE date <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
-- ORDER BY date
Btw, in case if you are still with Legacy SQL - see below example
#legacySQL
SELECT *
FROM -- yourTable
(SELECT 1 AS id, DATE('2017-04-20') AS date),
(SELECT 2 AS id, DATE('2017-04-21') AS date),
(SELECT 3 AS id, DATE('2017-04-22') AS date),
(SELECT 4 AS id, DATE('2017-04-23') AS date),
(SELECT 5 AS id, DATE('2017-04-24') AS date),
(SELECT 6 AS id, DATE('2017-04-25') AS date)
WHERE TIMESTAMP(date) <= DATE_ADD(TIMESTAMP(CURRENT_DATE()), -3, 'DAY')
-- ORDER BY date
This works with a string formatted date.
DATE(TIMESTAMP(date)) <= DATE_SUB(CURRENT_DATE(), INTERVAL 3 DAY)
Just tested this and seems to work.
I added this :
and DATE(TIMESTAMP(datevalue)) >= DATE_SUB(CURRENT_DATE(), INTERVAL 21 DAY)
and managed to get all records greater than last 21 days worth. Only thing I changed from #ericbrownaustin 's code was changed the 'date' in the first piece of code in the second set of parenthesis.