How to query for grouped results in Access, based on count of time and day of week? - sql

I have a table in an Access 2013 database, which has many rows of incidents, each of which has a "time" column and a "day_of_week" column. I need to query for a result set that shows a count of incidents during hourly time ranges from 6:00am-5:00pm, per day of the week. I can get a total count of incidents per time range like so:
SELECT "6:00AM - 6:59AM" AS [Time Range],COUNT(time) AS [Count]
FROM Incidents
WHERE time >= #6:00:00 AM# AND time <=#6:59:00 AM#
UNION
SELECT "7:00AM - 7:59AM" AS [Time Range],COUNT(time) AS [Count]
FROM Incidents
WHERE time >= #7:00:00 AM# AND time <=#7:59:00 AM#
UNION
...
So on, and so forth. It is a lengthy query, but it gets the task done. However, as stated previously, I need to drill down further to figure out how many incidents occurred specifically during each hourly time frame, per day of the week. So, I need to have a column for "time", and then an additional column for each day of the week. I know this will involve some grouping, but I am not certain what must be done.
This is my first post here. Hopefully I followed the rules.
Thanks

This query should do what you need, except that it will only show hour as an hour number, rather than the expanded description your query fragment above shows:
select day_of_week, datepart("h",[time]) as hour_num, count(*)
from incidents
group by day_of_week, datepart("h",[time])
hope this helps

Related

How to count input throughout the day averaged by weekdays

I want to get an output of the average amount of tasks solved by hour throughout the day.
But I want to get this average amount by an hour of the day throughout the week.
number of tasks
7AM (avg of all 7-8AM on Monday to Friday)
8AM
9AM
etc
select count(*),
date_trunc('hour', tasks) as "dateAxis",
FROM tasks
group by "dateAxis"
demo:db<>fiddle
date_trunc('hour', ...) only "cuts off" the minutes and seconds, so to speak. So you
are normalizing your datetime value to hours. The date will be kept. For example: 2010-01-02 12:23:42 will be converted to 2010-01-02 12:00:00. So, because the date will be kept, you cannot group throughout different dates but only throughout the related hour. Instead you could use extract('hour' from ...) which extracts the hour value of your datetime as a separate value which can be used for grouping throughout different days.
The following query groups all given dates be their hour:
select count(*),
extract('hour', tasks) as "dateAxis"
FROM tasks
group by "dateAxis"
If you only want to recognize all days of a certain week, you'll need to recognize the week number as as well in your group:
SELECT
count(*),
extract('week' from mydate) as week,
extract('hour' from mydate) as hour
FROM mytable
GROUP BY week, hour
Of course, if you are dealing with data sets which include different years, you should add the year component into your group as well.

Calculate the average time between two dates

I need to find the result of a calculation that is nothing more than the average time in days from creation to completion of a task.
In this case, using a Redshift database (looker).
I have two dates (2022/10/01 to 2022/10/21) and I need to find the average day of execution of the creation of an object from start to finish.
Previously, I was able to calculate the totals of objects created per day, but I can't bring up the average:
SELECT created::date, count(n1pk_package_id)
FROM dbt_dw.base_package
WHERE fk_company_id = 245821 and created >= '2022-10-01' and created < '2022-10-22'
GROUP BY created::date
ORDER BY created DESC
I'm not able to do the opposite way of the count to bring the average of the range of days.
Assumption:
There is a created column in your table
You want to know the 'average' of the created column
You could extract the number of days that each date is different from a base date, and then use that to determine the 'average date'. It would be something like this:
select
date '2022-10-01' + interval '1 day' * int(avg(created - date '2022-10-01'))
from table
It subtracts a date (any date will do) from created, finds the average of that value against all desired rows, converts it to days and adds it back to that same date.

how to get data for last calender week in redshift

I have a below query that I run to extract material movements from the last 7 days.
Purpose is to get the data for the last calender week for certain reports.
select
*
From
redshift
where
posting_date between CURRENT_DATE - 7 and CURRENT_DATE - 1
That means I need to run the query on every Monday to get the data for the former week.
Sometimes I am too busy on Monday or its vacation/bank holiday. In that case I would need to change the query or pull the data via SAP.
Question:
Is there a function for redshift that pulls out the data for the last calender week regardless when I run the query?
I already found following solution
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
But this doesnt seem to be working for redshift sql
Thanks a lot for your help.
Redshift offers a DATE_TRUNC('week', datestamp) function. Given any datestamp value, either a date or datetime, it gives back the date of the preceding Sunday.
So this might work for you. It filters rows from the Sunday before last, up until but not including, the last Sunday, and so gets a full week.
SELECT id
FROM table1
WHERE date >= DATE_TRUNC('week', NOW()) - INTERVAL 1 WEEK
AND date < DATE_TRUNC('week', NOW())
Pro tip: Every minute you spend learning your DBMS's date/time functions will save you an hour in programming.

Excel MDX - same period last month - totals

I have the following MDX expression which helps me to display the data for the 'same period last month' from the Cube PER DAY.
Filter(
[Date].[Date].[Date],
[Date].[Date].CurrentMember.Member_Value >=
DateSerial(Year(DateAdd('m', -1, VBA![Date]())),
Month(DateAdd('m', -1, VBA![Date]())),
1
)
AND [Date].[Date].CurrentMember.Member_Value < DateAdd('m', -1, VBA![Date]())
)
Now I would like to redo the code to display just the total for this period without breaking it down per day. The reason for this is, that if for example I want to see the total number of active users for the same period last month I can't simply sum up the numbers of daily users as this will be higher than the real number of total users in this period (user may be active on more than one day and I just want to count him once for this period).
I tried chaning the code but nothing works so far. Any ideas?
Many thanks in advance,
Maciej

use of week of year & subsquend in bigquery

I need to show distinct users per week. I have a date-visit column, and a user id, it is a big table with 1 billion rows.
I can change the date column from the CSVs to year,month, day columns. but how do I deduce the week from that in the query.
I can calculate the week from the CSV, but this is a big process step.
I also need to show how many distinct users visit day after day, looking for workaround as there is no date type.
any ideas?
To get the week of year number:
SELECT STRFTIME_UTC_USEC(TIMESTAMP('2015-5-19'), '%W')
20
If you have your date as a timestamp (i.e microseconds since the epoch) you can use the UTC_USEC_TO_DAY/UTC_USEC_TO_WEEK functions. Alternately, if you have an iso-formatted date string (e.g. "2012/03/13 19:00:06 -0700") you can call PARSE_UTC_USEC to turn the string into a timestamp and then use that to get the week or day.
To see an example, try:
SELECT LEFT((format_utc_usec(day)),10) as day, cnt
FROM (
SELECT day, count(*) as cnt
FROM (
SELECT UTC_USEC_TO_DAY(PARSE_UTC_USEC(created_at)) as day
FROM [publicdata:samples.github_timeline])
GROUP BY day
ORDER BY cnt DESC)
To show week, just change UTC_USEC_TO_DAY(...) to UTC_USEC_TO_WEEK(..., 0) (the 0 at the end is to indicate the week starts on Sunday). See the documentation for the above functions at https://developers.google.com/bigquery/docs/query-reference for more information.