I need to schedule a query on BigQuery, that will retrieve some data from 24h ago. The problem is that for example if its scheduled for 5am, the query needs time to be executed (let's say 2.248 seconds), and so the yesterday's data from 5:00:00.000 to 5:00:02.248 will not be retrieved. I'm using a timestamp field, and i do something like this :
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP_SUB(current_timestamp(), INTERVAL 24 hour)
I would like to know if there is a way to get the yesterday's date, at a specific hour, so even if there is a little gap due to the execution, it will still retrieve data from a specific hour.
edit:
I found something :
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP(DATE_SUB(current_date(), INTERVAL 1 DAY) )
AND my_timestamp < TIMESTAMP(current_date)
But this retrieves yesterday between 00:00:00.000 to 23:59:59.999
It is okay but is there a way to choose the hour ?
Consider below least verbose approach
select *
from my_table
where my_timestamp >= timestamp(current_date - 1 + interval 5 hour)
and my_timestamp < timestamp(current_date + interval 5 hour)
Okay I found on my own lol, maybe it will help someone else looking for this.
SELECT *
FROM my_table
WHERE my_timestamp >= TIMESTAMP_ADD(TIMESTAMP(DATE_SUB(current_date, INTERVAL 1 DAY)), INTERVAL 5 HOUR)
AND my_timestamp < TIMESTAMP_ADD(TIMESTAMP(current_date), INTERVAL 5 HOUR)
Related
I am having a bit of tough time wrapping my head around this. I have a column based on response time in hours and our company's SLA (service level agreement) is that all incoming inquires should be answered within 2 days (the response time in hours is total hours spent on responding to inquiry).
The problem is that our company operates with winter time (7 h 45 min) and summer time (7 h). My dataset consist both and I want Power BI to differietiate winter and summer time when I try to compute SLA. In winter time 1 working day = 7 h 45 min and in summer time = 7 h. I have just used the average of summer/winter time = 7 h 30 min. The SLA column consist 3 data types, "Innen en arbeidsdag", "Innen to arbeidsdager" and "over 2 arbeidsdager".
I have used the following syntax:
SLA = SWITCH(TRUE(),Response time in hours>15, "Over to arbeidsdager", esponse time in hours>7.5, "Innen to arbeidsdager", Response time in hours<=7.5, "Innen en arbeidsdag")
How can I use DATESBETWEEN in this syntax to tell Power BI that Response Time YTD column from 15th May to 15th September is summer time, Working day = 7 h?
Just as an idea, I wouldn't use DATESBETWEEN. If you want to input dates directly to logically categorize your work seasons, try this:
SWITCH(TRUE()
,'Date'[Date] >= DATE(2022,5,15)
&& 'Date'[Date] <= DATE(2022,9,15)
,7
,BLANK()--You can carry on with logic for other seasons
)
I am using this syntax on a date table, but you can do this with any table that has a date column.
My dataset is called 'Masterdata' and my columns looks like this:
Svartid i t SLA
6,12 Innen en arbeidsdag
11,73 Innen to arbeidsdager
20,42 Over to arbeidsdager
1,07 Innen en arbeidsdag
etc etc
My syntax so far:
SLA = SWITCH(TRUE(),Masterdata[Svartid i t]>15, "Over to arbeidsdager", Masterdata[Svartid i t]>7.5, "Innen to arbeidsdager", Masterdata[Svartid i t]<=7.5, "Innen en arbeidsdag")
So how can implement
SWITCH(TRUE(),'Date'[Date] >= DATE(2022,5,15) && 'Date'[Date] <= DATE(2022,9,15),7,BLANK()
In my syntax?
I am looking into creating a list, where I see how many IDs were generated in a given hour. Because I want to display the hours from 8 to 21 in advance independent of the hour of the date, I used generate series and now need to find the equivalent for Snowfake. This is my query:
series as (
SELECT seq4() as Hour
FROM TABLE(GENERATOR(rowcount => 21))
where Hour between 7 and 20
ORDER BY Hour),
ID_table as (
select extract(hour from date) as "Hour",
count(ID) as "Count"
from ID_table
group by 1)
select (Hour.Hour) + 1 AS "Hour",
id."Count",
from series as Hour
left join ID_table as id on id."Hour" = Hour.Hour
order by Hour.Hour;
For some reason I only get the Hours 8 to 16, however, I want it to display the hours 8-21, what could be the issue?
You should always consider that SEQ() functions do not guarantee gaps, so for generating the range, I suggest you to use ROW_NUMBER() function:
https://community.snowflake.com/s/article/Generate-gap-free-sequences-of-numbers-and-dates
Anyway, when I test it, I see it returns expected numbers:
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21));
-- returnns numbers from 0 to 20
SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 order by hour;
-- returnns numbers from 7 to 20
with series as (SELECT seq4() as Hour FROM TABLE(GENERATOR(rowcount => 21)) where Hour between 7 and 20 ORDER BY Hour)
select (Hour.Hour) + 1 AS "Hour"
from series as Hour;
-- returnns numbers from 8 to 21
Could it be something with the browser/UI?
I have a simple table contains the node, message, starttime, endtime details where starttime and endtime are in unix timestamp. The query I am running is:
select node, message, (select from_unixtime(starttime)), (select from_unixtime(endtime)) from table1 WHERE try(select from_unixtime(starttime)) > to_iso8601(current_timestamp - interval '24' hour) limit 100
The query is not working and throwing the syntax error.
I am trying to fetch the following information from the table:
query the table using start time and end time for past 'n' hours or 'n' days and get the output of starttime and endtime in human readable format
query the table using a specific date and time in human readable format
You don't need "extra" selects and you don't need to_iso8601 in the where clasue:
WITH dataset AS (
SELECT * FROM (VALUES
(1627409073, 1627409074),
(1627225824, 1627225826)
) AS t (starttime, endtime))
SELECT from_unixtime(starttime), from_unixtime(endtime)
FROM
dataset
WHERE from_unixtime(starttime) > (current_timestamp - interval '24' hour) limit 100
Output:
_col0
_col1
2021-07-27 18:04:33.000
2021-07-27 18:04:34.000
to search last week you can use
WHERE your_date >= to_unixtime(CAST(now() - interval '7' day AS timestamp))
I'm struggling to get the same period of last month.
I want to compare the current month period, for instance, today is 16June2021 so I want to get the sales from May 1st to May 16th.
I'm using this formula but I get the whole month total:
Prev MTD = calculate(sum(Sales[Sales_Amount]),DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH))
Creating a table with DATEADD(filter(DATESMTD(Sales[Sale_Date]),Sales[Sale_Date]<=today()),-1,MONTH), I also get every day of last month.
Is it mandatory to use a Date Table? Already tried but the results came empty.
Is it something regarding my date format? From the import it comes as date/time format.
Thank you very much
Try with:
Prev MTD = calculate(sum(Sales[Sales_Amount]),filter(ALL(Sales[Sale_Date]),Sales[Sale_Date] >= DATE( YEAR(TODAY()), MONTH(TODAY())-1, 1) && Sales[Sale_Date]<=today()))
where order_date between '2022-08-01' and DATE_SUB(CURRENT_DATE(),32)
last month 31 days that's why i am calculated 32 days.
if last month 30 day than you need to calculate 31 days.
I am trying to convert a row sql query. Here is my original row query that I want to run
select * from sales WHERE updated_at BETWEEN
date_add('2018-10-24', INTERVAL -10 HOUR)
and
date_add('2018-10-25', INTERVAL -10 HOUR)
I want to add this date_add functionality into this query
Sale.query().whereBetween('updated_at',[data.date1,data.date2])
.getSum('totalAmount')
Many thanks.
What you need is a whereRaw, your query will be the next
Sale.query().whereRaw('updated_at BETWEEN date_add('2018-10-24', INTERVAL -10 HOUR) and date_add('2018-10-25', INTERVAL -10 HOUR'))