I have the following data in MySQL:
+------------+-------+
| myDATE | delta |
+------------+-------+
| 2015-08-29 | 12 |
| 2015-08-30 | 12 |
| 2015-08-31 | 10 |
| 2015-09-01 | 0 |
| 2015-09-02 | 0.15 |
+------------+-------+
I want to run a query that will select the last day of each month and show the data. I thought I had it working but its not showing yesterdays data.
select mydate, delta from data group by date_format(mydate, '%Y-%m');
Results in:
+------------+-------+
| mydate | delta |
+------------+-------+
| 2015-08-29 | 12 |
| 2015-09-01 | 0 |
+------------+-------+
What am I missing?
I'm looking for it to return this: (ie. The last day of each month on record)
+------------+-------+
| myDATE | delta |
+------------+-------+
| 2015-08-31 | 10 |
| 2015-09-02 | 0.15 |
+------------+-------+
select last_day(mydate), sum(delta) from data group by last_day(mydate)
last_day is a function which returns the last day of the month the given date
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_last-day
to answer the updated question
select data.`myDate`, data.delta from data inner join
(select max(`myDate`) myDate from data group by date_format(mydate, '%Y-%m')) a
on a.myDate = data.myDate
Related
I have a query as below
SELECT * FROM salesData
WHERE date BETWEEN "2020-10-01" AND "2020-10-05"
ORDER BY date
will get result of
-------------------------
| sales | date |
-------------------------
| 150.00 | 2020-10-01 |
| 200.00 | 2020-10-04 |
| 250.00 | 2020-10-05 |
-------------------------
and I wish to get a result from every date in the range even it is empty data as below
-------------------------
| sales | date |
-------------------------
| 150.00 | 2020-10-01 |
| 0.00 | 2020-10-02 |
| 0.00 | 2020-10-03 |
| 200.00 | 2020-10-04 |
| 250.00 | 2020-10-05 |
-------------------------
is there any function in MySql able to do it?
WITH RECURSIVE
cte AS ( SELECT '2020-10-01' AS `date`
UNION ALL
SELECT `date` + INTERVAL 1 DAY
FROM cte
WHERE `date` < '2020-10-05' )
SELECT `date`, COALESCE(salesData.sales, 0) sales
FROM cte
LEFT JOIN salesData USING (`date`)
MySQL 8+ needed.
I have a table of subscriptions, storing user id, subscription end date, program id. One user can be subscribed to many programs, but for the scope of the problem the latest date is considered as the end date of the subscription. The goal is to find the number of users whose subscription is ending for each month of each year. To illustrate it:
-------------------------------------------
| user_id | program_id | end_date |
-------------------------------------------
| 1 | 1 | 2015-12-10 |
| 1 | 2 | 2017-08-27 |
| 2 | 1 | 2017-09-20 |
| 3 | 2 | 2017-10-01 |
| 2 | 3 | 2017-09-18 |
| 5 | 12 | 2017-10-22 |
| 4 | 3 | 2017-10-10 |
| 3 | 8 | 2018-11-15 |
-------------------------------------------
Intermediate result show when will the subscription end for each user (only month is needed):
------------------------------
| user_id | enddate |
------------------------------
| 1 | 2017-08 |
| 2 | 2017-09 |
| 3 | 2018-11 |
| 4 | 2017-10 |
| 5 | 2017-10 |
------------------------------
This was achieved with the query:
Select user_id, DATE_FORMAT(max(end_date), '%Y-%m') AS as enddate
From subscription
Group by user_id
Order by end_date desc;
The final result must further filter the list, showing only how many users will be left with no subscription in each month, like this:
------------------------------
| count | month, year |
------------------------------
| 1 | 2017-08 |
| 1 | 2017-09 |
| 2 | 2017-10 |
| 1 | 2018-11 |
------------------------------
This is where I am stuck with no mysql ideas. Iterating through the results and counting is out of the question.
You could try arranging the results by the enddate, like this:
select count(user_id), DATE_FORMAT(max_end_date, '%Y-%m')as enddate
from (
select user_id, max(end_date) as max_end_date
From subscription
Group by user_id
) n
group by enddate
Order by enddate desc;
Try this -
Select COUNT(*), DATE_FORMAT(MAX(end_date), '%Y-%m') AS as enddate
From subscription
Group by user_id
Order by end_date desc;
I have a database, and I've been trying for a while, but I'm not getting the results I want. Here's a sample of what I have:
+---------+---------------------+
| Ammount | Date |
+---------+---------------------+
| 1 | 2015-08-25 14:07:00 |
| 1 | 2015-08-25 14:12:00 |
| 1 | 2015-08-25 15:17:00 |
| 2 | 2015-08-25 15:22:00 |
| 1 | 2015-08-25 14:27:00 |
| 6 | 2015-08-25 14:32:00 |
| 1 | 2015-08-26 14:37:00 |
| 5 | 2015-08-26 14:42:00 |
| 1 | 2015-08-26 16:47:00 |
| 2 | 2015-08-26 16:52:00 |
+---------+---------------------+
And this is my query:
select Ammount, Date from table;
What I want to do is group by the day AND the hours of each day, and sum it, pretty much like this:
select sum(Ammount), Date from table group by hour(day(Date));
Except it groups everything together.
SELECT SUM(Ammount), Date
FROM TABLE
GROUP BY YEAR(Date),
MONTH(Date),
DAY(Date),
HOUR(Date);
select sum(Ammount), Date from table group by month(Date), day(Date), hour(Date);
I have a table like this which consists of several climatic measures (rain rates, temps, etc.)
mysql> select rain_rate, temperature, datetime from weather limit 10;
+--------------+---------------+----------------------+
| rain_rate | temperature | datetime |
+--------------+---------------+----------------------+
| 5.0000000000 | 24.4000000000 | 2017-02-08 16:00:56 |
| 1.0000000000 | 22.4000000000 | 2017-02-06 12:10:36 |
| 2.0000000000 | 28.7000000000 | 2017-02-02 13:57:15 |
| 5.0000000000 | 24.7000000000 | 2017-02-01 14:14:16 |
| 1.0000000000 | 16.1000000000 | 2017-01-08 06:01:26 |
| 2.0000000000 | 18.2000000000 | 2017-01-12 05:10:43 |
| 3.0000000000 | 11.9000000000 | 2017-01-10 06:20:54 |
| 4.0000000000 | 16.8000000000 | 2017-01-25 16:10:14 |
| 5.0000000000 | 24.4000000000 | 2016-12-18 23:10:56 |
| 4.0000000000 | 26.6000000000 | 2016-12-30 09:03:54 |
...
As can be seen, timestamps (datetime field) does not follow any pattern.
I want to get the last 24 average values of temperature and rain_rate by hour, and a column with the numerical value of the associated hour, in 24-hour format, ordered by hour asc.
As an example, if I executed the query today at 18:30 pm, it should return these 24 rows:
+-----------------+------------------+-------+
| avg(rain_rate) | avg(temperature) | hour |
+-----------------+------------------+-------+
| 3.5000000000 | 23.1000000000 | 19 | |
| 1.0000000000 | 22.6000000000 | 20 | |
| 3.5000000000 | 24.7000000000 | 21 | |-> hours of "yesterday"
| 4.5000000000 | 23.8000000000 | 22 | |
...
| 2.0000000000 | 26.3000000000 | 13 | |
| 1.5000000000 | 21.6000000000 | 14 | |
| 7.0000000000 | 23.4000000000 | 15 | |-> hours of "today"
| 2.5000000000 | 21.4000000000 | 16 | |
| 7.0000000000 | 21.2000000000 | 17 | |
| 3.0000000000 | 25.3000000000 | 18 | |
My best try so far:
select avg(rain_rate), avg(temperature), hour(datetime) as hour
from weather
where (datetime >= now() - interval 24 hour)
group by hour(datetime)
order by max(datetime) asc
It looks like that query returns the correct average values for the fields, but hour field does not seem to be ordered like I need nor corresponding to the mean values...
Any help is much appreciated.
Thanks in advance.
You want to average by hour for the past 24 hours.
Ok. Here is one way:
select date(datetime), hour(datetime),
avg(rain_rate), avg(temperature)
from weather
where (datetime >= now() - interval 24 hour)
group by date(datetime), hour(datetime)
order by min(datetime);
Note: 24 hours from the current time might be a little weird. You could get 25 rows of records (with two partial hours). You may want this where:
where datetime < curdate() + interval hour(now()) hour and
datetime >= curdate() + interval hour(now()) - 24 hour
I suppose you must try order by datetime.
The query must be like:
select avg(rain_rate), avg(temperature), hour(datetime) as hour
from weather
where (datetime >= now() - interval 24 hour)
group by hour(datetime)
order by datetime asc;
I hope i was help.
Hi All I have a result from mysql that looks like this:
+---------------------+------+
| timestamp | data |
+---------------------+------+
| 2015-06-01 04:25:18 | 10 |
| 2015-06-15 04:25:18 | 20 |
| 2015-06-30 04:25:18 | 30 |
| 2015-07-01 04:25:18 | 50 |
| 2015-07-15 04:25:18 | 60 |
| 2015-07-30 04:25:18 | 70 |
| 2015-08-01 04:25:18 | 80 |
| 2015-08-15 04:25:18 | 90 |
| 2015-08-30 04:25:18 | 100 |
+---------------------+------+
I can use a query to work out the delta so it is like this:
+---------------------+------+------+
| timestamp | data | delta |
+---------------------+------+------+
| 2015-06-01 04:25:18 | 10 | 0 |
| 2015-06-15 04:25:18 | 20 | 10 |
| 2015-06-30 04:25:18 | 30 | 10 |
| 2015-07-01 04:25:18 | 50 | 20 |
| 2015-07-15 04:25:18 | 60 | 10 |
| 2015-07-30 04:25:18 | 70 | 10 |
| 2015-08-01 04:25:18 | 80 | 10 |
| 2015-08-15 04:25:18 | 90 | 10 |
| 2015-08-30 04:25:18 | 100 | 10 |
+---------------------+------+------+
And what I am after is a grouping of the delta column by month:
+-------+-------------+
| month | consumption |
+-------+-------------+
| 6/15 | 20 |
| 7/15 | 40 |
| 8/15 | 30 |
+-------+-------------+
I thought I had to try GROUP BY MONTH(timestamp) but it does not aggregate the deltas.
Any thoughts?
EDIT
to clarify the delta is a calculated column here is a modified query to show you what i mean:
SELECT
node_time,
node_read - #prev AS delta,
#prev := waveflow_data.node_read
FROM
meter_data
INNER JOIN waveflow_data ON meter_data.node_address = waveflow_data.node_address
CROSS JOIN (SELECT #prev := (SELECT node_read FROM waveflow_data ORDER BY `node_time` DESC LIMIT 1)) variable_initialization_query_alias
WHERE
meter_data.node_address = '10164E998976'
ORDER BY waveflow_data.`node_time` DESC
You just need to sum(delta) like below:
select time_stamp,sum(delta) as Consumption from test
group by month(time_stamp);
Check SQL FIDDLE DEMO
If you want the final result from the initial given set of data then you can do as
select
date_format(timestamp,'%m/%y') as month,
sum(delta) as delta
from
(
select
timestamp,
data,
#diff:= if(#prev=0,0,(data - #prev)) as delta,
#prev:= data
from table_name,(select #prev:=0)x
order by timestamp
)x
group by month;
DEMO
Say if you have a query like, Select timestamp,data,delta from mytable groupby timestamp
update it to Select DATE_FORMAT(timestamp,'%m/%y') as timestamp,data,delta from mytable groupby timestamp
The timestamp would now be in your desired format and grouping would automatically be done based on that.