counting line per day - sql

Db HIve
I'm trying to count table line each day between 8 and 9, for every day after 01-11-2020.
I would like to have data like that:
Date, count
This is the query I have tried to run and I get one line instead of a line for every single day. Could you help me with what I have done wrong?
SELECT to_date(timestamp) as dates, count(*) as Records
from table
where to_date(timestamp) > '2020-11-01' and HOUR(timestamp) BETWEEN 8 and 9
GROUP BY to_date(timestamp)

If I remove the count(*) and group by - it's run, but it is not really what I am trying to achieve.
I need to have a count(*) for each date between 8 and 9 morning, that the query would run automatically each morning and give all the data from the 01-11-2020 until the date it's run.
When I run the query just with the hour (HOUR(timestamp) BETWEEN 8 and 9) it runs correctly, but not with the entire query.
Thanks

Presumably, you don't have data on all days during that hour period. If you do have data for all days, you can tweak the query using conditional aggregation:
SELECT to_date(timestamp) as dates,
SUM(CASE WHEN HOUR(timestamp) BETWEEN 8 and 9 THEN 1 ELSE 0 END) as Records
FROM table
WHERE to_date(timestamp) > '2020-11-01' and
GROUP BY to_date(timestamp);

Related

Exclude dates within a range in SQL/Big Query

I am trying to exclude any data that has more than 10 days between Min_Sort_Date and Max_Sort_Date.
This is the initial query I have to start with:
select
srt_bin_id,
Min(srt_cred_tmst) as Min_Sort_Date,
Max(srt_cred_tmst)as Max_Sort_Date,
Count(trkg_id) as count_of_packages,
Max(srt_loc_id) as Store
from bda-prd.dp_logistics.bqt_lgs_rets_rpt
where srt_cred_tmst > "2022-08-13"
Group by 1
Any tips on where to go from here?
If my memory is good, you can use DATE_DIFF function with BigQuery to do something like:
select
DATE_DIFF(Max(srt_cred_tmst), Min(srt_cred_tmst), DAY) AS DAYS_GAP from
Database.Table
Like that you will be able to calculate the gap, and exclude rows with a where condition when DAYS_GAP >= 10

How to write SQL statement to select for data broken up for each month of the year?

I am looking for a way to write an SQL statement that selects data for each month of the year, separately.
In the SQL statement below, I am trying to count the number of instances in the TOTAL_PRECIP_IN and TOTAL_SNOWFALL_IN columns when either column is greater than 0. In my data table, I have information for those two columns ("TOTAL_PRECIP_IN" and "TOTAL_SNOWFALL_IN") for each day of the year (365 total entries).
I want to break up my data by each calendar month, but am not sure of the best way to do this. In the statement below, I am using a UNION statement to break up the months of January and February. If I keep using UNION statements for the remaining months of the year, I can get the answer I am looking for. However, using 11 different UNION statements cannot be the optimal solution.
Can anyone give me a suggestion how I can edit my SQL statement to measure from the first day of the month, to the last day of the month for every month of the year?
select monthname(OBSERVATION_DATE) as "Month", sum(case when TOTAL_PRECIP_IN or TOTAL_SNOWFALL_IN > 0 then 1 else 0 end) AS "Days of Rain" from EMP_BASIC
where OBSERVATION_DATE between '2019-01-01' and '2019-01-31'
and CITY = 'Olympia'
group by "Month"
UNION
select monthname(OBSERVATION_DATE) as "Month", sum(case when TOTAL_PRECIP_IN or TOTAL_SNOWFALL_IN > 0 then 1 else 0 end) from EMP_BASIC
where OBSERVATION_DATE between '2019-02-01' and '2019-02-28'
and CITY = 'Olympia'
group by "Month"```
Your table structure is too unclear to tell you the exact query you will need. But a general easy idea is to build the sum of your value and then group by monthname and/or by month. Sice you wrote you only want sum values greater 0, you can just put this condition in the where clause. So your query will be something like this:
SELECT MONTHNAME(yourdate) AS month,
MONTH(yourdate) AS monthnr,
SUM(yourvalue) AS yoursum
FROM yourtable
WHERE yourvalue > 0
GROUP BY MONTHNAME(yourdate), MONTH(yourdate)
ORDER BY MONTH(yourdate);
I created an example here: db<>fiddle
You might need to modify this general construct for your concrete purpose (maybe take care of different years, of NULL values etc.). And note this is an example for a MYSQL DB because you wrote about MONTHNAME() which is in most cases used in MYSQL databases. If you are using another DB type, maybe you need to do some modifications. To make sure that answers match your DB type, tag it in your question, please.

How to run a query for multiple independent date ranges?

I would like to run the below query that looks like this for week 1:
Select week(datetime), count(customer_call) from table where week(datetime) = 1 and week(orderdatetime) < 7
... but for weeks 2, 3, 4, 5 and 6 all in one query and with the 'week(orderdatetime)' to still be for the 6 weeks following the week(datetime) value.
This means that for 'week(datetime) = 2', 'week(orderdatetime)' would be between 2 and 7 and so on.
'datetime' is a datetime field denoting registration.
'customer_call' is a datetime field denoting when they called.
'orderdatetime' is a datetime field denoting when they ordered.
Thanks!
I think you want group by:
Select week(datetime), count(customer_call)
from table
where week(datetime) = 1 and week(orderdatetime) < 7
group by week(datetime);
I would also point out that week doesn't take the year into account, so you might want to include that in the group by or in a where filter.
EDIT:
If you want 6 weeks of cumulative counts, then use:
Select week(datetime), count(customer_call),
sum(count(customer_call)) over (order by week(datetime)
rows between 5 preceding and current row) as running_sum_6
from table
group by week(datetime);
Note: If you want to filter this to particular weeks, then make this a subquery and filter in the outer query.

ACCESS: Get total time different and amount of time ranges above a set amount

I have a table that I'm trying to not only get the sum of time(hours) difference between two columns but also the amount of times a time difference is above a set amount, 6 in this case.
The total I got from Getting the sum of a datediff result but can I in the same query also get count(*) where datediff => 6?
Thanks in advance for any and all help.
DateDiff used for hours will probably not be useful, as it will return 1 hour from, say 10:55 to 11:03.
So count minutes:
Select
*, DateDiff("n", [TimeStart], [TimeEnd]) / 60 As Hours
From
YourTable
Save this query and use it as source in a new query to count those entries with an hour count greater than or equal to six:
Select Count(*) As Entries
From YourQuery
Where Hours >= 6

SELECT statement optimization

I'm not so expert in SQL queryes, but not even a complete newbie.
I'm exporting data from a MS-SQL database to an excel file using a SQL query.
I'm exporting many columns and two of this columns contain a date and an hour, this are the columns I use for the WHERE clause.
In detail I have about 200 rows for each day, everyone with a different hour, for many days. I need to extract the first value after the 15:00 of each day for more days.
Since the hours are different for each day i can't specify something like
SELECT a,b,hour,day FROM table WHERE hour='15:01'
because sometimes the value is at 15:01, sometimes 15:03 and so on (i'm looking for the closest value after the 15:00), for fix this i used this workaround:
SELECT TOP 1 a,b,hour,day FROM table WHERE hour > "15:00"
in this way i can take the first value after the 15:00 for a day...the problem is that i need this for more days...for a user-specifyed interval of days. At the moment i fix this with a UNION ALL statement, like this:
SELECT TOP 1 a,b,hour,day FROM table WHERE data="first_day" AND hour > "15:00"
UNION ALL SELECT TOP 1 a,b,hour,day FROM table WHERE data="second_day" AND hour > "15:00"
UNION ALL SELECT TOP 1 a,b,hour,day FROM table WHERE data="third_day" AND hour > "15:00"
...and so on for all the days (i build the SQL string with a for each day in the specifyed interval).
Until now this worked, but now I need to expand the days interval (now is maximun a week, so 5 days) to up to 60 days. I don't want to build an huge query string, but i can't imagine an alternative way for write the SQL.
Any help appreciated
Ettore
I typical solution for this uses row_number():
SELECT a, b, hour, day
FROM (SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY day ORDER BY hour) as seqnum
FROM table t
WHERE hour > '15:00'
) t
WHERE seqnum = 1;