SQL group: count multiple things - mysql

Right now I've got the following (My)SQL-Statement which returns the amount of entrys based on hour.
SELECT
COUNT(*) AS amount
HOUR(date) AS hour
-- [1]
FROM
table
GROUP BY
HOUR(date)
But I actually want another result that contains the amount of days the hour appeared. Basicly something like:
[1] = COUNT(DAY(date), MONTH(date), YEAR(date)) AS day_count
Example:
id | date
0 | 01/01/2001 5:15
1 | 01/01/2001 5:10
2 | 01/01/2001 6:03
3 | 01/01/2001 7:04
4 | 02/01/2001 5:00
Should return
amount | hour | day_count
3 | 5 | 2
1 | 6 | 1
1 | 7 | 1

I think you just need to part out the days by hour and day, and group by them...
SELECT count(*) as `amount`
, count(hour(date)) as `Hour`
, count(day(date)) as `Day_Count`
FROM table
GROUP BY hour(Date), day(date)

Related

How to select some rows as columns in mysql

Hello guys? I am working with a MySQL table that stores employees payno, date and meter readings for electricity. I want to select readings for a previous month as previous month reading and current month reading as current month reading, this is what I mean
payno | date | value
-------+------------+------------
6 | 2019-08-31 | 2477
6 | 2019-09-25 | 2487
8 | 2019-08-31 | 1651.2
8 | 2019-09-25 | 1697.6
Above is the structure of my table, I want to achieve something like this:
payno | previous month| current month
-------+---------------+------------
6 | 2477 | 2487
8 | 1651.2 | 1697.6
Notice that the previous month reading are the readings having month(date) as 8 and the current month readings have month(date) as 9.
I don't know how many readings can be per month, so we'll summarize them.
select e.payno,
( select sum(x1.val)
from emp x1
where x1.payno=e.payno and month(x1.data)=month(date_sub(e.data, INTERVAL 1 MONTH))
group by x1.payno ) AS prev_month,
( select sum(x2.val)
from emp x2
where x2.payno=e.payno and month(x2.data)=month(e.data)
group by x2.payno ) AS this_month
from emp e
where month(e.data)=9 /* this is where you specify "current" month */
;

SQL comparing column changes the last X hours

I have a table containing log entries that looks like this:
id | name | level | timestamp
5 | ironman | 35 | 2019-01-06 11:37:40
6 | ironman | 35 | 2019-01-06 11:38:40
7 | ironman | 36 | 2019-01-06 11:39:40
8 | superman | 25 | 2019-01-06 11:39:49
I need help making a query that returns a list of levels gained the last X hours for each character, preferably sorted by amount gained.
So by this example my wanted result is this
id | name | gained | timestamp
7 | ironman | 1 | 2019-01-06 11:37:40
8 | superman | 0 | 2019-01-06 11:37:40
You need to join the main table with a query that calculates the change in levels:
select levels.id, t.name, t.gained, t.timestamp
from (
select
name,
max(level) - min(level) gained,
max(timestamp) timestamp
from levels
where timestamp > now() - interval 10 hour
group by name
) t inner join levels
on
t.timestamp = levels.timestamp
and
t.name = levels.name
where levels.timestamp > now() - interval 10 hour
order by t.gained desc, t.name
I guess the timestamp in the expected output you posted about superman is wrong and it should be 2019-01-06 11:39:49.
See the demo
Try an aggregate query that compares the MIN and MAX level of each character :
SELECT
name,
MAX(level) - MIN(level) as gained,
MIN(timestamp)
FROM mytable
WHERE timestamp > NOW() - INTERVAL 1 HOUR
GROUP BY
name
ORDER BY
gained desc
NB : this assumes that the level of a character may only increase. If the level of a character decreases, it will still appear as a gain.

Finding total active hours by calculating difference between TimeDate records

I have a table to register users logs every one minute and other activities using DateTime for each user_id
This is a sample data of my table
id | user_id | log_datetime
------------------------------------------
1 | 1 | 2016-09-25 13:01:08
2 | 1 | 2016-09-25 13:04:08
3 | 1 | 2016-09-25 13:07:08
4 | 1 | 2016-09-25 13:10:08
5 | 2 | 2016-09-25 13:11:08
6 | 1 | 2016-09-25 13:13:08
7 | 2 | 2016-09-25 13:13:09
8 | 2 | 2016-09-25 13:14:10
I would like to calculate the total active time on the system
UPDATE: Expected Output
For Example user_id 1 his total available time should be 00:12:00
Since his hours and seconds are same so I'll just subtract last log from previous then previous from next previous and so on then I'll sum all subtracted values
this a simple for
Simply I want to loop through the data from last record to first record with in my range
this is a simple formula I hope that make my question clear
SUM((T< n > - T< n-1 >) + (T< n-1 > - T< n-2 >) ... + (T< n-x > - T< n-first >))
Since user_id 1 his hours and seconds are the same then I'll calculate the minutes only.
(13-10)+(10-7)+(7-4)+(4-1) = 12
user_id | total_hours
---------------------------------
1 | 00:12:00
2 | 00:03:02
I did this code
SET #start_date = '2016-09-25';
SET #start_time = '13:00:00';
SET #end_date = '2016-09-25';
SET #end_time = '13:15:00';
SELECT
`ul1`.`user_id`, SEC_TO_TIME(SUM(TIME_TO_SEC(`dl1`.`log_datetime`))) AS total_hours
FROM
`users_logs` AS `ul1`
JOIN `users_logs` AS `ul2`
ON `ul1`.`id` = `ul2`.`id`
WHERE
`ul1`.`log_datetime` >= CONCAT(#start_date, ' ', #start_time)
AND
`ul2`.`log_datetime` <= CONCAT(#end_date, ' ', #end_time)
GROUP BY `ul1`.`user_id`
But this code Sum all Time not getting the difference. This is the output of the code
user_id | total_hours
---------------------------------
1 | 65:35:40
2 | 39:38:25
How can I calculate the Sum of all difference datetime, then I want to display his active hours every 12 hours (00:00:00 - 11:59:59) and (12:00:00 - 23:59:59) with in selected DateTime Period at the beginning of the code
So the output would look like this (just an dummy example not from given data)
user_id | total_hours | 00_12_am | 12_00_pm |
-------------------------------------------------------
1 | 10:10:40 | 02:05:20 | 08:05:20 |
2 | 04:10:20 | 01:05:10 | 03:05:30 |
Thank you
So you log every minute and if a user is available there is a log entry.
Then count the logs per user, so you have the number of total minutes.
select user_id, count(*) as total_minutes
from user_logs
group by user_id;
If you want them displayed as time use sec_to_time:
select user_id, sec_to_time(count(*) * 60) as total_hours
from user_logs
group by user_id;
As to conditional aggregation:
select
user_id,
count(*) as total_minutes,
count(case when hour(log_datetime) < 12 then 1 end) as total_minutes_am,
count(case when hour(log_datetime) >= 12 then 1 end) as total_minutes_pm
from user_logs
group by user_id;
UPDATE: In order to count each minute just once count distinct minutes, i.e. DATE_FORMAT(log_datetime, '%Y-%m-%d %H:%i'). This can be done with COUNT(DISTINCT ...) or with a subquery getting distinct values.
The complete query:
select
user_id,
count(*) as total_minutes,
count(case when log_hour < 12 then 1 end) as total_minutes_am,
count(case when log_hour >= 12 then 1 end) as total_minutes_pm
from
(
select distinct
user_id,
date_format(log_datetime, '%y-%m-%d %h:%i') as log_moment,
hour(log_datetime) as log_hour
from.user_logs
) log
group by user_id;

MySQL: Calculate 4 Week Average with 4 Week Offset

This question is related to a post I made earlier: MySQL: Calculating Data from Table with One Month Offset
But now I need to build a procedure that queries a table of contact data stored by week. Here's a simplified example of the table I am working with:
+-----------------+------------+
| week_start_date | contacts |
+-----------------+------------+
| 2015-03-08 | 12 |
| 2015-03-01 | 20 |
| 2015-02-22 | 5 |
| 2015-02-15 | 17 |
| 2015-02-08 | 8 |
| 2015-02-01 | 2 |
| 2015-01-25 | 16 |
| 2015-01-18 | 10 |
| 2015-01-11 | 4 |
| ... | ... |
+-----------------+------------+
What I need to figure out is how to calculate a 4 week moving average that also has a 4 week offset on top of that. For instance, if I wanted to get the average contacts for the week of March 8, 2015, it would be the average of January 18 through February 8. In the example above, my average would be: (10 + 16 + 2 + 8 ) / 4 = 9. And if I wanted to find the average for the week of March 1, 2015, then it would be the average of January 11 through February 1 which comes out to be 8 using the sample table above.
From my last post, I know that I can handle the 4 week offset by joining the table with itself on the week_start_date similar to this:
SELECT s1.week_start_date, s2.Total_Contacts
FROM sample_table s1
LEFT JOIN (SELECT week_start_date, sum(contacts) AS Total_Contacts
FROM sample_table
GROUP BY week_start_date) s2
ON s1.week_start_date =
date_add(s2.week_start_date, INTERVAL 4 WEEK)
WHERE s1.week_start_date = '2015-03-08'
GROUP BY s1.week_start_date;
But getting it to compute the four week average as well is where I am stuck. I thought joining it on a range of dates would work, but I keep getting averages that are a lot larger than expected. I'm guessing it is due to how the week_start_date's are being grouped. (Note that there can be multiple records for each week. I only show one record for each week on the sample table to make it less cluttered.)
Is joining on a date range the correct approach? Or do I need to add another join somewhere?
Thanks for your help.
I would suggest using a correlated subquery:
select st.*,
(select avg(contacts)
from sample_table st2
where st2.week_start_date >= st.week_start_date - interval 7 * 7 days and
st2.week_start_date <= st.week_start_date - interval 4 * 7 days
) as avg_4week_delayed
from sample_table st;
I would use the DATE_SUB() function and just subtract the necessary weeks you need. So, for March 8 in your example, try something like this:
SELECT AVG(contacts)
FROM myTable
WHERE week_start_date <= DATE_SUB('2015-03-08', INTERVAL 4 WEEK) AND week_start_date >= DATE_SUB('2015-03-08', INTERVAL 7 WEEK);
It worked in SQL Fiddle.

count the number of rows between intervals

My table is like:
+---------+---------+------------+-----------------------+---------------------+
| visitId | userId | locationId | comments | time |
+---------+---------+------------+-----------------------+---------------------+
| 1 | 3 | 12 | It's a good day here! | 2012-12-12 11:50:12 |
+---------+---------+------------+-----------------------+---------------------+
| 2 | 3 | 23 | very beautiful | 2012-12-12 12:50:12 |
+---------+---------+------------+-----------------------+---------------------+
| 3 | 3 | 52 | nice | 2012-12-12 13:50:12 |
+---------+---------+------------+-----------------------+---------------------+
which records visitors' trajectory and some comments on the places visited.
I want to count the numbers of visitors that visit a specific place (say id=3227) from 0:00 to 23:59, over some interval (ie. 30mins)
I was trying to do this by :
SELECT COUNT(*) FROM visits
GROUP BY HOUR(time), SIGN( MINUTE(time) - 30 )// if they are in the same interval this will yield the same result
WHERE locationId=3227
The problem is that if there is no record that falls in some interval, this will NOT return that interval with count 0. For example, there are no visitors visiting the location from 02:00 to 03:00, this will not give me the intervals of 02:00-02:29 and 02:30-2:59.
I want a result with an exact size of 48 (one for every half hour), how can I do this?
You have to create a table with the 48 rows that you want and use left outer join:
select n.hr, n.hr, coalesce(v.cnt, 0) as cnt
from (select 0 as hr, -1 as sign union all
select 0, 1 union all
select 1, -1 union all
select 1, 1 union all
. . .
select 23, -1 union all
select 23, 1 union all
) left outer join
(SELECT HOUR(time) as hr, SIGN( MINUTE(time) - 30 ) as sign, COUNT(*) as cnt
FROM visits
WHERE locationId=3227
GROUP BY HOUR(time), SIGN( MINUTE(time) - 30 )
) v
on n.hr = v.hr and n.sign = v.sign
order by n.hr, n.hr