SQL - Returning max count, after breaking down a day into hourly rows - sql

I need to write a SQL query that helps return the highest count in a given hourly range. The problem is that in my table, it just logs orders as they come and doesn’t have a unique identifier that separates hours from hours.
So basically, I need to find the highest number of orders (on any given hour), from 7/08/2022, - 7/15/2022, have a table that does not distinguish distinct hour sets, and logs orders as they come.
I have tried to use a query that combines MAX(), COUNT(), and DATETIME(), but to no avail.
Can I please receive some help?

I've had to tackle this kind of measurement in the past..
Here's what I did for 15 minute intervals:
My datetime column is named datreg in my database log area.
cast(round(floor(cast(datreg as float(53))*24*4)/(24*4),5) as smalldatetime
I times by 4 in this formula, to get 4 intervals inside my 24 hour period.. For you it would look like this to get just hourly intervals:
cast(round(floor(cast(datreg as float(53))*24)/(24),5) as smalldatetime
This is a little piece of magic when it comes to dashboards and reports.

Related

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)

Creating custom timestamp buckets bigquery

I have an hourly (timestamp) dataset of events from the past month.
I would like to check the performance of events that occurred between certain hours, group them together and average the results.
For example: AVG income of the hours 23:00-02:00 per user:
So if I have this data set below. I'd like to summarise the coloured rows and then average them (the result should be 218).
I tried NTILE but it couldn't divide the data properly, ignoring the irrelevant hours.
Is there a good way to create these custom buckets using SQL?
dataset
From description not exactly sure how you want to aggregate. If you provide an example dataset can update answer.
However you can easily achieve this with an AVG and IF statement.
AVG(IF(EXTRACT(HOUR FROM timestamp_field) BETWEEN 0 AND 4, value, NULL) as avg_value
Using the above you can then group by either day or month to get the aggregation level you want.

Subtracting Date/Time from two different tables in SQL

I have two tables with a time column (year-day-month hr:min:sec)
Let's say name of table 1 is plc and column name Collect
Name of table2 is Adm and column name Disc
I want to subtract the time of Collect (2005-01-03 18:10:05) from the disc column (2005-01-03 20:15:10) in day, hours, minutes.
Any help would be appreciated!
I would surely go for the datediff function.
Have a look at this link:
https://msdn.microsoft.com/en-us/library/ms189794(v=sql.90).aspx
If you want, you can select difference in minutes, and then, with the minutes, calculate the days and hours
Use Date() or Datepart() function to fetch the specific part of the date and subtract the two.
You can get more details of the above two at the following link:
https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql

Sql Queries for finding the sales trend

Suppose ,I have a table which has all the billing records. Now I want to see the sales trend for a user given time duration group by each 3 days ...what should be the sql query regarding this?
please help,Otherwise I am gone ...
I can only give a vague suggestion as per the question, however you may want to have a derived column with a standardised date (as per MS date format, just a number per day) that you could then use a modulus (3) on so that days are equal per 3 day period. You can then group and aggregate over this column to get the values for a 3 day period. Obviously to display the date nicely you would have to multiply back and convert your column as well.
Again I'm not sure of the specifics, but I think this general idea could be achieved to get a result (may well not be the best way so it would help to add more to the question...)

Query to find a weekly average

I have an SQLite database with the following fields for example:
date (yyyymmdd fomrat)
total (0.00 format)
There is typically 2 months of records in the database. Does anyone know a SQL query to find a weekly average?
I could easily just execute:
SELECT COUNT(1) as total_records, SUM(total) as total FROM stats_adsense
Then just divide total by 7 but unless there is exactly x days that are divisible by 7 in the db I don't think it will be very accurate, especially if there is less than 7 days of records.
To get a daily summary it's obviously just total / total_records.
Can anyone help me out with this?
You could try something like this:
SELECT strftime('%W', thedate) theweek, avg(total) theaverage
FROM table GROUP BY strftime('%W', thedate)
I'm not sure how the syntax would work in SQLite, but one way would be to parse out the date parts of each [date] field, and then specifying which WEEK and DAY boundaries in your WHERE clause and then GROUP by the week. This will give you a true average regardless of whether there are rows or not.
Something like this (using T-SQL):
SELECT DATEPART(w, theDate), Avg(theAmount) as Average
FROM Table
GROUP BY DATEPART(w, theDate)
This will return a row for every week. You could filter it in your WHERE clause to restrict it to a given date range.
Hope this helps.
Your weekly average is
daily * 7
Obviously this doesn't take in to account specific weeks, but you can get that by narrowing the result set in a date range.
You'll have to omit those records in the addition which don't belong to a full week. So, prior to summing up, you'll have to find the min and max of the dates, manipulate them such that they form "whole" weeks, and then run your original query with a WHERE that limits the date values according to the new range. Maybe you can even put all this into one query. I'll leave that up to you. ;-)
Those values which are "truncated" are not used then, obviously. If there's not enough values for a week at all, there's no result at all. But there's no solution to that, apparently.