How to add a minutes column to date column sql - sql

I may sound silly asking it - but I have the need to add a value derived from one column of database to the other which is the date.
Following is the image of data I have - timeelapsed/60000 gives me the duration of task in minutes and I would require the same to be deducted from the lastupdatedtime to know the time when was the task picked. Please help!
I have attempted as per your suggestion
lastupdatedtime - (timeelapsed/60000) * interval '1 minute' as task_pick_datetime,
But I see the difference appearing is huge.
For instance, the audited time is 2020-03-12 17:41:39.224, duration in min is 3.06 but the task pick time shows 2020-03-12 12:08:35.417 which is 5 hours difference. Unsure where am I going wrong.
enter image description here

In Standard SQL (which is the tag on your question), you can use:
select lastupdatetime - (timeelapsed/60000) * interval '1 minute' as task_pick_datetime
However, date/time functions are notoriously database dependent, so your database may not support this syntax.

Related

Finding the Closest Unbooked Dates Using SQL

Scenario
A user selects a date. Based on the selection I check whether the date & time is booked or not (No issues here).
If a date & time is booked, I need to show them n alternative dates. Based on their date and time parameters, and those proposed alternative dates have to be as close as to their chosen date as possible. The list of alternative dates should start from the date the query is ran on My backend handles this.
My Progress So Far
SELECT alternative_date
FROM GENERATE_SERIES(
TIMESTAMP '2022-08-20 05:00:00',
date_trunc('month', TIMESTAMP '2022-08-20 07:00:00') + INTERVAL '1 month - 1 day',
INTERVAL '1 day'
) AS G(alternative_date)
WHERE NOT EXISTS(
SELECT * FROM events T
WHERE T.bookDate::DATE = G.alternative_date::DATE
)
The code above uses the GENERATE_SERIES(...) function in PSQL. It searches for all dates, starting from 2022-08-20, and up to the end of August. It specifically returns the dates which does not exist in the bookDate column (Meaning it has not yet been booked).
Problems I Need Help With
When searching for alternative dates, I'm providing 3 important things
The user's preferred booking date, so I can suggest which other dates are close to him that he can choose? How would I go about doing this? It's the part where I'm facing most trouble.
The user's start and end times, so when providing a list of alternative dates, I can tell him, hey there's free space between 06 and 07 on the date 2022-08-22 for instance. I'm also facing some issues here, a push in the right track will be great!
I want to add another WHERE but it fails, the current WHERE is a NOT EXISTS so it looks for all dates not equaling to what is given. My other WHERE basically means WHERE the place is open for booking or not.
To get closest free dates, you can ORDER BY your result by "distance" of particular alternative date to user's preferred date - the shortest intervals will be first:
ORDER BY alternative_date - TIMESTAMP '2022-08-20 05:00:00'
If you want to recommend time slots smaller than whole dates (hour range), you need to switch the whole thing from dates to hours, i.e. generate_series from 1 day to 1 hour (or whatever your smallest bookable unit is) and excluse invalid hours (nighttime I assume) in WHERE. From there, it is pretty much the same as with dates.
As for "second where", there can be only one WHERE, but it can be composed from multiple conditions - you can add more conditions using AND operator (and it can also be sub-query if needed):
WHERE NOT EXISTS(
SELECT * FROM events T
WHERE T.bookDate::DATE = G.alternative_date::DATE
) AND NOT EXISTS (
SELECT 1 FROM events WHERE "roomId" = '13b46460-162d-4d32-94c0-e27dd9246c79'
)
(warning: this second sub-query is probably dangerous in real world, since the room will be used more than one time, I assume, so you need to add some time condition to the subquery to check against date)

Group Timestamps in minute intervals in AMC SQL - Amazon Marketing Cloud

I am trying to grab event timestamps event_dt from an Amazon Marketing Cloud table sponsored_ads_traffic
and in the output group impressions in continuous n minute interval buckets using an SQL statement.
Reasoning for grouping:
The AMC uses Data Privacy Filters which will remove all columns in the output if certain criteria on the amount of users per row are not met (e.g. 100 unique user_id per row), which makes it impossible to use full timestamps in the output. This filter is reflected in two columns that are appended by AMC automatically ( privacy_user_count, privacy_is_filtered.)
I need to find the smallest possible timeframe that will return the most amount of data by looking at the results from 1,5,10,15 etc. minute intervals. I understand there is probably also a way to look at the timestamps and user_ids and see which interval is needed to satisfy >100 user_ids but my SQL level is not good enough for that yet and so I decided to compare set intervals.
Here is a sample of the desired output, using 5 minute intervals.
timestamp_bucket
sum_impressions
privacy_user_count
privacy_is_filtered
2022-04-03T06:30:00.000Z
143
130
false
2022-04-03T06:35:00.000Z
167
146
false
2022-04-03T06:40:00.000Z
156
132
false
93
true
I can then compare the sum of total impressions over the same timespan from a separate query with the sum of impression not filtered in this query to gauge how much data is lost in each grouping interval and find the smallest interval with the most amount of data.
The AMC product is currently in BETA and so far only available to select agencies and partners. I am unfortunately unable to share the full documentation and thus will try to provide neccesary information as needed. AMC SQL is limited in expressions and similar to PostgreSQL in syntax.
Event Timestamps follow the format: 2022-04-03T06:32:17.453Z
I was looking at similar questions and answers and have compiled a few hopefully relevant expressions below.
Extract- Get time unit from DATE or TIMESTAMP value. Time unit can be one of SECOND, MINUTE,
HOUR, DAY, DOW, WEEK, MONTH, YEAR.
EXTRACT(timeUnit FROM expression)
Seconds Between - Return number of seconds between two inputs temporal date types.
SECONDS_BETWEEN(firstInputTime, secondInputTime)
Date Trunc - Truncate time to specified level of granularity. Granularity must be single quoted and can be
one of SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, YEAR.
DATE_TRUNC('granularity', inputTime)

Split date_start and date_end by hours on Metabase

I have table with a column "Begin At" and another column "End At" that represent when a task begin and when a task end i would want to have a Bar display which display the cuantity of tasks that are being done in a specific hour along an interval of time.
For example, from the following table
I would want to be able to see that from 07/12/2021 21:00 to 07/12/2021 22:00 there were 3 tasks being done (row 1, row 2, row 3).
And also as i will have several thousands of rows i would want to use the date widget from metabase in order to specify range of times.
I have been struggling with this from the last week, i tried to create auxiliar questions where to query after but finally my only succeed was to hard code the 24 hours from a day but then i was not able to use the time widget and i needed to specify the dates myself on the sql each time i want to check a specific day and also i only was able to check from 24 to 24 hours, not from for example 02/12/2021 6:00 to 04/12/2021 18:00
My metabase is running on a PostgreSQL database. Is this even possible on Metabase? If not what are your advices to build this? Other plaforms? Pure SQL? Python?
Thank you so much
I am not sure about metabase but from a PostgreSQL point of view this calls for the use of range-types, specifically the tsrange/tstzrange, depending on whether you have time zone information or not.
So a query could be:
SELECT
*
FROM "someTable"
WHERE
tsrange("Begin At", "End At", '[)')
&&
tsrange('02/12/2021 6:00', '04/12/2021 18:00', '[)')
However I don't know how you would get the '02/12/2021 6:00' and '04/12/2021 18:00' out of your metabase user-interface.

How to make the database delete a row after certain time on the row?

I'm doing a project about reporting situations on a map. Each report is marked by a pin on the map. The pin table has an attribute, report_time, that saves the hour in which the pin was inserted (or in which the report was made).
How can I make a function/trigger/procedure (whatever is the best option) to delete that row after 15 minutes since the report? I want something on my database that gets the report_time and deletes the row if 15 minutes have passed.
Instead of constantly deleting rows, how about just using a view?
create view v_pin as
select p.*
from pin p
where p.report_time > now() - interval '15 minute;
Anyone who uses the view will only see the most recent rows.
Then, when the database is quiescent or when you want -- say once an hour or once a day or once a week, just schedule a job to remove old rows:
delete from pin
where report_time < now() - interval '15 minute';
What is elegant about this solution is that users will never see inappropriate rows in the table, even if the job does not run as scheduled or the table is locked for updates for some reason.

Difference between Timestamp is 15 minutes

This is the CREATED_TIME 2012-07-17 00:00:22 and this is the Corresponding Timestamp 1342508427000. Here timestamp is 5 seconds more than the CREATED_TIME. I need to see below scenario
Currently I have a query, in which I am joining on created_time and timestamp like this-
ON (UNIX_TIMESTAMP(testingtable1.created_time) = (prod_and_ts.timestamps / 1000))
So in above case, it will not match as timestamp is 5 seconds more than created_time. But I need if the difference between either of the two is within 15 minutes then I need to match it.
So I wrote the below JOIN query- I am not sure whether this is the right way to do it or not?
ON ((UNIX_TIMESTAMP(testingtable1.created_time) - (prod_and_ts.timestamps / 1000)) / 60* 1000 <= 15)
How I can do the above case if difference between timestamps is within 15 minutes then data will get matched by the above ON clause
I'd prefer the (specifically designed for this purpose!) date and time functions instead of doing al these kinds of calculations with timestamps. You wouldn't believe how much trouble this can cause. Make sure you read and understand this and this