Find the difference between current time and last failure in grafana? - sql

I have a dashboard built up where i am able to fetch the last failure time through conditions such as (total_failed<>0) but that gives me output as time like 10:20 but I need to have the Output generated in form of "last failed 30 mins ago " by calculating (current time - failure time ) so that. I can show that in dashboard .
Query Given -
SELECT
end_time AS "time",
(total_failed::integer)
FROM job
where
total_failed<>'0'
GROUP BY end_time,total_failed
Let me know if anyone can help me writing that sql or representation of that dashboard .

Perhaps this combination of age, extract and a CTE is what you need:
WITH data AS (
SELECT age(clock_timestamp(), end_time) AS ago,
total_failed::integer AS fail_count
FROM job
WHERE ...
)
SELECT extract(minutes FROM ago)
+ 60 * extract(hours FROM ago) AS minutes_passed,
fail_count
FROM data;

Related

Running a Count on an Interval

I'm trying to do an alert of sorts for customers joining. The alert needs to run on an interval of one hour, which is possible with an integration we have.
The sample data is this:
Name
Time
John
2022-04-21T13:49:51
Mary
2022-04-23T13:49:51
Dave
2022-04-25T13:49:51
Gregg
2022-04-27T13:49:51
so the problem with the below output is this only captures the "count" within the hour. And will yield no results. But I'm trying to determine the moment (well, within the hour) the threshold crosses above a count of 3. Is there something I'm missing?
SELECT COUNT (name)
FROM Table
WHERE Time >= TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -60 MINUTE)
HAVING COUNT (NAME) > 3

Get data when date is equal to or greater than 90 days ago

I wonder if anyone here can help with a BigQuery piece I am working on.
I'm trying to pull the date, email and last interaction time from a dataset when the last interaction time is equal to or greater than 90 days ago.
I have the following query:
SELECT
date,
user_email,
DATE_FROM_UNIX_DATE(gmail.last_interaction_time) AS Last_Interaction_Date,
DATE_ADD(CURRENT_DATE(), INTERVAL -90 DAY) AS Days_ago
FROM
`bqadminreporting.adminlogtracking.usage`
WHERE
'Last_Interaction_Date' >= 'Days_ago'
However, I run into the following error:
DATE value is out of allowed range: from 0001-01-01 to 9999-12-31
As far as I can see, it makes sense - so not entirely sure why its throwing out an error?
Looks like you have some inconsistent values (data) in filed gmail.last_interaction_time, which you need to handle to avoid error.
Moreover above query will not work as per your expected WHERE conditions, you should use following query to get expected output.
SELECT * FROM
(SELECT
date,
user_email,
DATE_FROM_UNIX_DATE(gmail.last_interaction_time) AS Last_Interaction_Date,
DATE_ADD(CURRENT_DATE(), INTERVAL -90 DAY) AS Days_ago
FROM
`bqadminreporting.adminlogtracking.usage`)
WHERE
Last_Interaction_Date >= Days_ago
Presumably, your problem is DATE_FROM_UNIX_DATE(). Without sample data, it is not really possible to determine what the issue is.
However, you don't need to convert to a date to do this. You can do all the work in the Unix seconds space:
select u.*
from `bqadminreporting.adminlogtracking.usage` u
where gmail.last_interaction_time >= unix_seconds(timestamp(current_date)) - 90 * 60 * 60 * 24
Note that I suspect that the issue is that last_interaction_time is really measured in milliseconds or microseconds or some other unit. This will prevent your error, but it might not do what you want.

Get data that is no more than an hour old in BigQuery

Trying to use the statement:
SELECT *
FROM data.example
WHERE TIMESTAMP(timeCollected) < DATE_ADD(USEC_TO_TIMESTAMP(NOW()), 60, 'MINUTE')
to get data from my bigquery data. It seems to return same set of result even when time is not within the range. timeCollected is of the format 2015-10-29 16:05:06.
I'm trying to build a query that is meant to return is data that is not older than an hour. So data collected within the last hour should be returned, the rest should be ignored.
Using Standard SQL:
SELECT * FROM data
WHERE timestamp > TIMESTAMP_ADD(CURRENT_TIMESTAMP(), INTERVAL -60 MINUTE)
The query you made means "return to me anything that has a collection time smaller than an hour in the future" which will literally mean your whole table. You want the following (from what I got through your comment, at least) :
SELECT *
FROM data.example
WHERE TIMESTAMP(timeCollected) > DATE_ADD(USEC_TO_TIMESTAMP(NOW()), -60, 'MINUTE')
This means that any timeCollected that is NOT greater than an hour ago will not be returned. I believe this is what you want.
Also, unless you need it, Select * is not ideal in BigQuery. Since the data is saved by column, you can save money by selecting only what you need down the line. I don't know your use case, so * may be warranted though
To get table data collected within the last hour:
SELECT * FROM [data.example#-3600000--1]
https://cloud.google.com/bigquery/table-decorators
Using Standard SQL:
SELECT * FROM data WHERE timestamp > **TIMESTAMP_SUB**(CURRENT_TIMESTAMP(), INTERVAL 60 MINUTE)

SQL Date Calculation formatting

It's been a while since I posted. I have an issue regarding date calculations.
I am trying to find the difference between two dates as in start time and finish time.
I have been able to find the difference in days so for instance if I have the dates:
start = 12/11/2014 12:05:05
finish = 13/11/2014 09:44:19
then the query gives me -0.90224537......
However, I need the answer in the form of hours, minutes, seconds for wage purposes. What is the best way of doing this?
My query so far is:
select
time_sheet.time_sheet_id,
time_sheet.start_date_time - time_sheet.finish_date_time,
employee_case.case, employee_case.employee
from
time_sheet
inner join
employee_case on time_sheet.employee_case = employee_case.employee_case_id
where
employee_case.case = 1;
P.S. I am using an Oracle database :)
date - date yields the difference in days. So you can use the standard time conversion to convert it to hours,minutes and seconds as below:
select
time_sheet.time_sheet_id,
(time_sheet.finish_date_time - time_sheet.start_date_time)||' days'||(time_sheet.finish_date_time - time_sheet.start_date_time)*24||' hours '||(time_sheet.finish_date_time - time_sheet.start_date_time)*24*60||' minutes '||(time_sheet.finish_date_time - time_sheet.start_date_time)*24*60*60||' seconds ' ,
employee_case.case, employee_case.employee
from
time_sheet
inner join
employee_case on time_sheet.employee_case = employee_case.employee_case_id
where
employee_case.case = 1;
Also, you would need finish_date_time as the minuend and start_date_time as the subtrahend to avoid negative values.
Thanks for the help toddlermenot but i have decided to go another way I have found. For anybody else who has the same issue I have done the following:
select
time_sheet.time_sheet_id,
to_number( to_char(to_date('1','J') +
(time_sheet.finish_date_time - time_sheet.start_date_time), 'J') - 1) days,
to_char(to_date('00:00:00','HH24:MI:SS') +
(time_sheet.finish_date_time - time_sheet.start_date_time), 'HH24:MI:SS') time,
employee_case.case, employee_case.employee
from
time_sheet
inner join
employee_case on time_sheet.employee_case = employee_case.employee_case_id
where
employee_case.case = 1;
This seems to do exactly what I need. It does the days in a serperate field to the hours minutes and seconds but for my purposes this is acceptable

Oracle timestamp difference greater than X hours/days/months

I am trying to write a query to run on Oracle database. The table ActionTable contains actionStartTime and actionEndTime columns. I need to find out which action took longer than 1 hour to complete.
actionStartTime and actionEndTime are of timestamp type
I have a query which gives me the time taken for each action:
select (actionEndTime - actionStartTime) actionDuration from ActionTable
What would be my where clause that would return only actions that took longer than 1 hour to finish?
Subtracting two timestamps returns an interval. So you'd want something like
SELECT (actionEndTime - actionStartTime) actionDuration
FROM ActionTable
WHERE actionEndTime - actionStartTime > interval '1' hour