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)
Related
I want to pull orders that have been placed in between (last 5 mins - last 10 mins). For that I am running a query:
$query="SELECT * FROM orders where `orderStatus`='PARTIAL' and `timeCreated` >= date_sub(now(),interval 10 minute)";
However, this query is picking data from between (last 0 mins - last 10 mins).
Can you please help what should I try?
Just add another filtering condition:
SELECT *
FROM orders
WHERE
orderStatus = 'PARTIAL'
AND timeCreated >= date_sub(now(), interval 10 minute)
AND timeCreated < date_sub(now(), interval 5 minute);
Assuming that you are using MySQL (the syntax of your existing query suggests it), you can also phrase the query using the following date arithmetics, which I find more straight-forward:
SELECT *
FROM orders
WHERE
orderStatus = 'PARTIAL'
AND timeCreated >= now() - interval 10 minute
AND timeCreated < now() - interval 5 minute
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
)
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.
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'
I wanna get rows in recent date, this week or month, etc. suppose the table has a field named: product_date.
To get the rows in the last month, you could use something like:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 MONTH);
Or for the last week:
SELECT * FROM table WHERE product_date >= DATE_SUB(NOW(), INTERVAL 1 WEEK);
For within the last seven days:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -7 DAY) AND NOW()
For the last month:
WHERE product_date BETWEEN DATE_ADD(NOW(), INTERVAL -1 MONTH) AND NOW()
While mopoke's solution gets results from last 30 or 7 days, this gives you results for current month only:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%c-%Y') = DATE_FORMAT(CURDATE(), '%c-%Y');
Or for current week:
SELECT * FROM table WHERE DATE_FORMAT(product_date, '%u-%Y') = DATE_FORMAT(CURDATE(), '%u-%Y');