MYSQL Average datetime - mysql

I have a following SQL statement and it generates the relevant output correctly (I want to group every 3 minutes values) :
SELECT date_time date, UNIX_TIMESTAMP(date_time) AS time_value,
FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3) AS minute_value, ph1_active_power AS p1
FROM powerpro1
GROUP BY date_time
Generated output :
+-----------+---------------------+------------+--------------+---------+
| record_no | date | time_value | minute_value | p1 |
+-----------+---------------------+------------+--------------+---------+
| 1 | 2014-12-01 00:00:00 | 1417372200 | 0 | 73.0767 |
| 2 | 2014-12-01 00:01:00 | 1417372260 | 0 | 73.0293 |
| 3 | 2014-12-01 00:02:00 | 1417372320 | 0 | 72.9818 |
| 4 | 2014-12-01 00:03:00 | 1417372380 | 1 | 72.9343 |
| 5 | 2014-12-01 00:04:00 | 1417372440 | 1 | 72.8868 |
| 6 | 2014-12-01 00:05:00 | 1417372500 | 1 | 72.8392 |
| 7 | 2014-12-01 00:06:00 | 1417372560 | 2 | 72.7916 |
| 8 | 2014-12-01 00:07:00 | 1417372620 | 2 | 72.744 |
| 9 | 2014-12-01 00:08:00 | 1417372680 | 2 | 72.6963 |
| 10 | 2014-12-01 00:09:00 | 1417372740 | 3 | 72.6486 |
| 11 | 2014-12-01 00:10:00 | 1417372800 | 3 | 72.6009 |
| 12 | 2014-12-01 00:11:00 | 1417372860 | 3 | 72.5531 |
| 13 | 2014-12-01 00:12:00 | 1417372920 | 4 | 72.5053 |
| 14 | 2014-12-01 00:13:00 | 1417372980 | 4 | 72.4575 |
| 15 | 2014-12-01 00:14:00 | 1417373040 | 4 | 72.4096 |
| 16 | 2014-12-01 00:15:00 | 1417373100 | 5 | 72.3617 |
| 17 | 2014-12-01 00:16:00 | 1417373160 | 5 | 72.3137 |
| 18 | 2014-12-01 00:17:00 | 1417373220 | 5 | 72.2657 |
| 19 | 2014-12-01 00:18:00 | 1417373280 | 6 | 72.2177 |
| 20 | 2014-12-01 00:19:00 | 1417373340 | 6 | 72.1697 |
| 21 | 2014-12-01 00:20:00 | 1417373400 | 6 | 72.1216 |
| 22 | 2014-12-01 00:21:00 | 1417373460 | 7 | 72.0734 |
| 23 | 2014-12-01 00:22:00 | 1417373520 | 7 | 72.0253 |
| 24 | 2014-12-01 00:23:00 | 1417373580 | 7 | 71.9771 |
+-----------+---------------------+------------+--------------+---------+
But, I want to get the average of time_value and the average of p1 and then need to GROUP by minute_ value. If I used above query for that with the relevant changes as follows,
SELECT date_time date, AVG(UNIX_TIMESTAMP(date_time)) AS time_value, FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3) AS minute_value, ROUND(AVG(ph1_active_power),4) AS p1
FROM powerpro1
GROUP BY minute_value
I got the incorrect out put as mentioned below.
+-----------+---------------------+-----------------+--------------+--------+
| record_no | date | time_value | minute_value | p1 |
+-----------+---------------------+-----------------+--------------+--------+
| 1 | 2014-12-01 00:00:00 | 1418754688.6364 | 0 | 2.2622 |
| 4 | 2014-12-01 00:03:00 | 1418754868.6364 | 1 | 2.2541 |
| 7 | 2014-12-01 00:06:00 | 1418755048.6364 | 2 | 2.246 |
| 10 | 2014-12-01 00:09:00 | 1418755228.6364 | 3 | 2.2378 |
| 13 | 2014-12-01 00:12:00 | 1418755408.6364 | 4 | 2.2297 |
| 16 | 2014-12-01 00:15:00 | 1418755588.6364 | 5 | 2.2216 |
| 19 | 2014-12-01 00:18:00 | 1418755768.6364 | 6 | 2.2134 |
| 22 | 2014-12-01 00:21:00 | 1418755948.6364 | 7 | 2.2052 |
+-----------+---------------------+-----------------+--------------+--------+
Required Output :
+-----------+---------------------+--------------+------------+---------+
| record_no | time_value | minute_value | time_value | p1 |
+-----------+---------------------+--------------+------------+---------+
| 2 | 2014-12-01 00:01:00 | 0 | 1417372260 | 73.0293 |
| 5 | 2014-12-01 00:04:00 | 1 | 1417372440 | 72.8868 |
| 8 | 2014-12-01 00:07:00 | 2 | 1417372620 | 72.744 |
| 11 | 2014-12-01 00:10:00 | 3 | 1417372800 | 72.6009 |
| 14 | 2014-12-01 00:13:00 | 4 | 1417372980 | 72.4575 |
+-----------+---------------------+--------------+------------+---------+
What may be the wrong.
Can anyone help me using the valuable time and knowledge.

can you try this?
SELECT date_time date, SUM(UNIX_TIMESTAMP(date_time))/COUNT(record_no) AS time_value, FLOOR((MINUTE(date_time) + (HOUR(date_time)*60))/3)*3 AS minute_value, ROUND((SUM(ph1_active_power)/COUNT(record_no)),4) AS p1
FROM powerpro1
GROUP BY minute_value
I have done it by the following query :
SELECT record_no, date_time,
ROUND(AVG(UNIX_TIMESTAMP(date_time))) AS time_value,
ROUND(AVG(ph1_active_power),4) AS p1
FROM powerpro1
WHERE date_time <= '2014-12-20 00:00:00'
GROUP BY date_time DIV 300

Related

Left join tables on nearest datetime without duplicating values

I am attempting to join table_a and table_b together on their nearest/closest datetime fields (date_a and date_b), but I am wanting to ensure that I do not receive duplicate values from table_b for each joined row. If the available date_b rows from table_b are used up on closer table_a values, then the joined row should just remain blank.
Another way of putting it: the datetime values from table_b can only be used once, and they should only be used on the absolute closest/nearest value from table_a.
Here's an example of table_a:
| entry_a | date_a |
|---------|---------------------|
| 1 | 2019-02-20 01:05:00 |
| 2 | 2019-02-20 01:10:00 |
| 3 | 2019-02-21 01:15:00 |
| 4 | 2019-02-22 01:20:00 |
| 5 | 2019-02-23 01:25:00 |
| 6 | 2019-02-24 01:30:00 |
| 7 | 2019-02-25 01:35:00 |
| 8 | 2019-02-26 01:40:00 |
| 9 | 2019-02-27 01:45:00 |
| 10 | 2019-02-28 01:50:00 |
Here's table_b:
| entry_b | date_b | filename |
|---------|---------------------|----------------|
| 1 | 2019-02-20 01:03:00 | 20190220010300 |
| 2 | 2019-02-20 01:07:00 | 20190220010700 |
| 3 | 2019-02-23 01:23:00 | 20190223012300 |
| 4 | 2019-02-24 01:26:00 | 20190224012600 |
| 5 | 2019-02-25 01:30:00 | 20190225013000 |
| 6 | 2019-02-26 01:34:00 | 20190226013400 |
| 7 | 2019-02-27 01:40:00 | 20190227014000 |
| 8 | 2019-02-28 01:50:00 | 20190228015000 |
| 9 | 2019-02-28 01:51:00 | 20190228015100 |
And here's the desired result:
| entry_a | date_a | entry_b | date_b | filename |
|---------|---------------------|---------|---------------------|----------------|
| 1 | 2019-02-20 01:05:00 | 1 | 2019-02-20 01:03:00 | 20190220010300 |
| 2 | 2019-02-20 01:10:00 | 2 | 2019-02-20 01:07:00 | 20190220010700 |
| 3 | 2019-02-21 01:15:00 | (null) | (null) | (null) |
| 4 | 2019-02-22 01:20:00 | 3 | 2019-02-23 01:23:00 | 20190223012300 |
| 5 | 2019-02-23 01:25:00 | 4 | 2019-02-24 01:26:00 | 20190224012600 |
| 6 | 2019-02-24 01:30:00 | 5 | 2019-02-25 01:30:00 | 20190225013000 |
| 7 | 2019-02-25 01:35:00 | 6 | 2019-02-26 01:34:00 | 20190226013400 |
| 8 | 2019-02-26 01:40:00 | 7 | 2019-02-27 01:40:00 | 20190227014000 |
| 9 | 2019-02-27 01:45:00 | 8 | 2019-02-28 01:50:00 | 20190228015000 |
| 10 | 2019-02-28 01:50:00 | 9 | 2019-02-28 01:51:00 | 20190228015100 |
One thing to particularly note in the desired result: the last two rows show that date_b.8 and date_a.10 match exactly ... but if date_b.8 and date_a.9 are allowed to match, then date_b.9 and date_a.10 can match on something fairly close, also. (If this is an impossible complication, I understand. It's not critical. What's more important is the situation illustrated in rows 2-4 of the result_table.)
I am using MySQL 5.6. I've built a SQL fiddle here with the tables loaded up: DEMO
Thank you all very kindly for your help and for the many answers you've provided to guide me over the years.

MYSQL: Display one query from a select statement from two unrelated tables

I'm trying to display the sum of monthly revenue from different unrelated tables, Where i display the budgeted against the actual usage
I have two tables Planned Budget and actual budget, with with similar columns but different data, in a query i want retrieve the sum of budget and group it Yearly and monthly.
Planned Budget table
+-----+-------+-------------+-------------+
| uid | codes | opca_budget | date_period |
+-----+-------+-------------+-------------+
| 10 | 3210 | 3000 | 2018-03-01 |
| 17 | 3355 | 3000 | 2018-03-01 |
| 33 | 3210 | 4000 | 2018-04-01 |
| 40 | 3355 | 4000 | 2018-04-01 |
| 56 | 3210 | 5000 | 2018-05-01 |
| 63 | 3355 | 5000 | 2018-05-01 |
| 79 | 3210 | 6000 | 2018-06-01 |
| 86 | 3355 | 6000 | 2018-06-01 |
| 109 | 3355 | 45000 | 2018-07-01 |
Actual Budget
+-----+-------+-------------+---------------------+
| uid | codes | opca_budget | date_period |
+-----+-------+-------------+---------------------+
| 10 | 3210 | 6500 | 2018-03-01 00:00:00 |
| 17 | 3355 | 6500 | 2018-03-01 00:00:00 |
| 18 | 3120 | 6500 | 2018-03-01 00:00:00 |
| 33 | 3210 | 7500 | 2018-04-01 00:00:00 |
| 40 | 3355 | 7500 | 2018-04-01 00:00:00 |
| 41 | 3120 | 7500 | 2018-04-01 00:00:00 |
| 56 | 3210 | 8500 | 2018-05-01 00:00:00 |
| 63 | 3355 | 8500 | 2018-05-01 00:00:00 |
| 64 | 3120 | 8500 | 2018-05-01 00:00:00 |
I tried to combine them horizontally, But i get wrong results
SELECT YEAR(c.date_period)
, MONTH(c.date_period)
, SUM(c.opca_budget) MonthlyBudget
, SUM(a.opca_budget) MonthlyUsage
FROM opexcapex c
LEFT
JOIN opxcpx_actuals a
ON YEAR(c.date_period) = YEAR(a.date_period)
AND MONTH(c.date_period) = MONTH(a.date_period)
WHERE c.date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP
BY YEAR(c.date_period)
, MONTH(c.date_period);
+------+-------+---------------+--------------+
| Year | Month | MonthlyBudget | MonthlyUsage |
+------+-------+---------------+--------------+
| 2018 | 3 | 168000 | 364000 |
| 2018 | 4 | 224000 | 420000 |
| 2018 | 5 | 280000 | 476000 |
| 2018 | 6 | 336000 | 532000 |
| 2018 | 7 | 2520000 | 588000 |
| 2018 | 8 | 576000 | 367200 |
| 2018 | 9 | 3240000 | 367200 |
| 2018 | 10 | 720000 | 252000 |
| 2018 | 11 | 792000 | 1807200 |
| 2018 | 12 | 2583000 | 1323000 |
| 2019 | 1 | 9000 | NULL |
| 2019 | 2 | 36000 | NULL |
+------+-------+---------------+--------------+
I expect to get results like this:
+-----------+-------------+---------------+--------------+
| BudgtYear | BudgetMonth | MonthlyBudget | MonthlyUsage |
+-----------+-------------+---------------+--------------+
| 2018 | 3 | 24000 | 45500 |
| 2018 | 4 | 32000 | 52500 |
| 2018 | 5 | 40000 | 59500 |
| 2018 | 6 | 48000 | 66500 |
| 2018 | 7 | 360000 | 73500 |
| 2018 | 8 | 72000 | 40800 |
| 2018 | 9 | 405000 | 40800 |
| 2018 | 10 | 90000 | 28000 |
| 2018 | 11 | 99000 | 200800 |
| 2018 | 12 | 369000 | 147000 |
| 2019 | 1 | 9000 | |
| 2019 | 2 | 36000 | |
+-----------+-------------+---------------+--------------+
You want to do a UNION ALL as a sub-query and then calculate the sum on the value from that sub-query, notice also that I use two amount columns in the union but one is always null, this is to have the two selects return the same number of columns but separated values
SELECT YEAR(date_period) as year, MONTH(date_period) as month, SUM(mbudget) AS MonthlyBudget, SUM(musage) as MonthlyUsage
FROM (SELECT date_period, opca_budget mbudget, null musage
FROM opexcapex
UNION ALL
SELECT date_period, null, opca_budget
FROM opxcpx_actuals
) sq
WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY year, month
You should be able to UNION the 2 tables first, then SELECT from the resulting data, something like this (Updated query) :-
SELECT YEAR(date_period), MONTH(date_period), MonthlyBudget, MonthlyUsage
FROM
(SELECT date_period, opca_budget as MonthlyBudget, 0 as MonthlyUsage FROM
opexcapex
UNION
SELECT date_period, 0 as MonthlyBudget, opca_budget as MonthlyUsage FROM
opxcpx_actuals) T1
WHERE date_period BETWEEN '2018-03-01' AND '2019-02-29'
GROUP BY YEAR(date_period), MONTH(date_period);

SQL Query handling NULLs

I have this database with this records
+-----+---------------------+---------+---------+
| id | since | idUsers | km |
+-----+---------------------+---------+---------+
| 124 | 2005-07-18 15:00:00 | 1 | 25798.0 |
| 127 | 2005-07-19 18:00:00 | 3 | 25891.7 |
| 128 | 2005-07-20 00:00:00 | 3 | 25970.2 |
| 129 | 2005-07-18 12:00:00 | 3 | 25795.0 |
| 136 | 2005-07-19 15:00:00 | 1 | 25852.0 |
| 137 | 2005-07-23 12:00:00 | 1 | 26143.6 |
| 139 | 2005-08-07 00:00:00 | 1 | NULL |
| 140 | 2005-08-21 00:00:00 | 1 | NULL |
| 146 | 2005-07-27 15:00:00 | 2 | 26164.0 |
| 147 | 2005-07-28 15:00:00 | 2 | 26178.9 |
| 151 | 2005-08-17 13:00:00 | 1 | 26245.0 |
| 149 | 2005-08-08 18:00:00 | 1 | 26204.9 |
| 150 | 2005-08-10 15:00:00 | 1 | 26221.4 |
| 155 | 2005-08-27 19:00:00 | 1 | NULL |
| 154 | 2005-08-29 09:00:00 | 2 | 26438.3 |
| 156 | 2005-08-28 00:00:00 | 1 | NULL |
| 157 | 2005-08-29 14:00:00 | 3 | 26468.3 |
| 158 | 2005-09-07 15:00:00 | 2 | 26504.2 |
| 159 | 2005-09-05 13:00:00 | 1 | 26478.5 |
| 160 | 2005-09-02 13:00:00 | 1 | 26473.8 |
| 161 | 2005-09-25 12:00:00 | 3 | 26550.4 |
| 162 | 2006-04-02 13:00:00 | 3 | 26599.0 |
| 163 | 2006-04-14 18:00:00 | 1 | NULL |
| 164 | 2006-04-15 00:00:00 | 1 | NULL |
| 169 | 2006-04-16 00:00:00 | 1 | 26703.3 |
| 166 | 2006-04-29 12:00:00 | 1 | NULL |
| 168 | 2006-04-30 00:00:00 | 1 | NULL |
| 170 | 2006-04-16 08:00:00 | 3 | 26709.0 |
| 175 | 2006-05-27 00:00:00 | 3 | NULL |
| 176 | 2006-05-28 00:00:00 | 3 | 27072.0 |
| 177 | 2006-05-26 18:00:00 | 3 | NULL |
| 178 | 2006-04-18 15:00:00 | 2 | 26751.0 |
| 179 | 2006-04-16 17:00:00 | 2 | 26726.0 |
| 181 | 2006-04-23 07:00:00 | 3 | 26775.0 |
| 182 | 2006-05-01 00:00:00 | 1 | 26932.4 |
| 183 | 2006-04-30 18:00:00 | 1 | NULL |
| 184 | 2006-05-11 17:00:00 | 2 | 26988.0 |
| 186 | 2006-06-12 12:00:00 | 2 | 27092.0 |
| 187 | 2006-06-11 19:00:00 | 1 | NULL |
| 188 | 2006-06-12 00:00:00 | 1 | NULL |
| 189 | 2006-06-15 17:00:00 | 2 | 27134.0 |
| 191 | 2006-07-01 11:00:00 | 3 | 27199.0 |
| 192 | 2006-06-23 16:00:00 | 3 | 27162.0 |
| 193 | 2006-07-09 15:00:00 | 2 | 27211.0 |
| 194 | 2006-06-30 18:00:00 | 1 | 27183.0 |
| 195 | 2006-07-26 11:00:00 | 2 | NULL |
| 196 | 2006-08-15 14:00:00 | 2 | 27351.0 |
| 197 | 2006-08-05 16:00:00 | 3 | 27338.0 |
| 198 | 2006-08-06 16:00:00 | 3 | 27341.0 |
| 199 | 2006-09-05 18:00:00 | 3 | NULL |
| 201 | 2006-09-06 00:00:00 | 3 | 27506.0 |
| 202 | 2006-09-16 13:00:00 | 1 | NULL |
| 203 | 2006-09-17 00:00:00 | 1 | NULL |
| 204 | 2006-09-18 00:00:00 | 1 | NULL |
| 205 | 2006-09-19 00:00:00 | 1 | 27568.6 |
| 206 | 2006-10-10 18:00:00 | 1 | 27569.4 |
| 207 | 2006-10-30 15:00:00 | 1 | 27581.5 |
| 209 | 2006-12-29 18:00:00 | 2 | NULL |
| 216 | 2007-04-01 08:00:00 | 1 | 27725.0 |
| 227 | 2007-04-26 00:00:00 | 2 | NULL |
| 214 | 2007-03-29 09:00:00 | 1 | 27675.5 |
| 215 | 2007-03-31 10:00:00 | 1 | 27689.2 |
| 225 | 2007-04-15 08:00:00 | 1 | 27880.3 |
| 224 | 2007-04-14 19:00:00 | 1 | 27800.0 |
| 223 | 2007-04-14 12:00:00 | 1 | 27775.0 |
| 228 | 2007-04-26 23:00:00 | 2 | NULL |
| 229 | 2007-04-27 00:00:00 | 2 | 28000.4 |
| 230 | 2007-04-28 00:00:00 | 2 | NULL |
| 231 | 2007-04-25 12:00:00 | 2 | NULL |
| 232 | 2007-04-20 18:00:00 | 1 | 27906.3 |
| 233 | 2007-04-22 15:00:00 | 1 | 27928.6 |
| 234 | 2007-04-21 17:00:00 | 1 | 27915.2 |
| 236 | 2007-04-23 19:00:00 | 1 | 28068.0 |
| 237 | 2007-05-01 07:00:00 | 3 | 28103.0 |
| 238 | 2007-04-28 18:00:00 | 1 | 28030.3 |
| 239 | 2007-05-20 11:00:00 | 1 | 28174.2 |
| 240 | 2007-05-20 15:00:00 | 2 | 28181.0 |
| 241 | 2007-05-23 16:00:00 | 2 | 28198.0 |
| 242 | 2007-06-03 16:00:00 | 2 | 28238.0 |
| 243 | 2007-06-10 09:00:00 | 3 | NULL |
| 244 | 2007-07-01 12:00:00 | 2 | 28291.0 |
| 245 | 2007-07-07 15:00:00 | 2 | 28343.0 |
| 246 | 2007-07-30 15:00:00 | 1 | 28354.8 |
| 247 | 2007-07-31 12:00:00 | 1 | 28476.9 |
| 248 | 2007-08-01 15:00:00 | 1 | NULL |
| 249 | 2007-08-04 11:00:00 | 3 | NULL |
| 250 | 2007-08-05 00:00:00 | 3 | NULL |
| 251 | 2007-08-05 10:00:00 | 3 | NULL |
| 252 | 2007-08-06 00:00:00 | 3 | 28708.0 |
| 253 | 2007-08-09 17:00:00 | 1 | 28729.9 |
| 254 | 2007-08-24 19:00:00 | 2 | 28806.0 |
| 255 | 2007-09-01 10:00:00 | 2 | 28812.0 |
| 256 | 2007-09-16 13:00:00 | 3 | 28842.0 |
| 257 | 2007-10-16 02:00:00 | 2 | NULL |
| 258 | 2008-06-19 12:00:00 | 1 | NULL |
| 259 | 2008-06-20 00:00:00 | 1 | NULL |
| 260 | 2008-06-21 00:00:00 | 1 | NULL |
| 261 | 2008-06-22 00:00:00 | 1 | NULL |
| 262 | 2008-06-23 00:00:00 | 1 | NULL |
| 263 | 2008-06-24 00:00:00 | 1 | 28986.7 |
| 264 | 2008-06-26 18:00:00 | 2 | 28997.0 |
| 265 | 2008-06-29 13:00:00 | 2 | 28998.0 |
| 266 | 2008-06-29 18:00:00 | 3 | 29012.0 |
| 267 | 2008-07-08 16:00:00 | 2 | 29019.0 |
| 268 | 2008-07-28 18:00:00 | 2 | 29082.0 |
| 269 | 2008-08-10 15:00:00 | 2 | 29192.0 |
| 270 | 2008-08-21 17:00:00 | 2 | NULL |
| 271 | 2008-08-24 13:00:00 | 2 | 29281.0 |
| 272 | 2008-08-27 21:00:00 | 3 | NULL |
| 273 | 2008-08-28 00:00:00 | 3 | NULL |
| 274 | 2008-08-29 00:00:00 | 3 | NULL |
| 275 | 2008-08-30 00:00:00 | 3 | 29343.0 |
| 276 | 2008-08-30 18:00:00 | 2 | 29352.0 |
| 277 | 2008-09-05 17:00:00 | 2 | 29385.0 |
| 279 | 2008-10-10 14:00:00 | 1 | NULL |
| 280 | 2008-10-11 00:00:00 | 1 | NULL |
| 281 | 2008-10-12 00:00:00 | 1 | NULL |
| 282 | 2008-10-13 00:00:00 | 1 | 29459.0 |
| 283 | 2009-04-05 10:00:00 | 2 | 29460.0 |
| 286 | 2009-04-19 10:00:00 | 3 | 29471.0 |
| 285 | 2009-05-02 00:00:00 | 1 | NULL |
| 287 | 2009-04-22 15:00:00 | 2 | 29486.0 |
| 288 | 2009-05-01 10:00:00 | 1 | NULL |
| 290 | 2009-05-20 15:00:00 | 3 | NULL |
| 289 | 2009-05-03 00:00:00 | 1 | 29668.6 |
| 291 | 2009-05-21 00:00:00 | 3 | NULL |
| 292 | 2009-05-22 00:00:00 | 3 | NULL |
| 293 | 2009-05-23 00:00:00 | 3 | NULL |
| 294 | 2009-05-22 09:00:00 | 3 | NULL |
| 295 | 2009-05-24 00:00:00 | 3 | 29820.0 |
| 296 | 2009-05-31 15:00:00 | 2 | 29830.0 |
| 297 | 2009-06-07 16:00:00 | 1 | 29850.3 |
| 298 | 2009-07-05 00:00:00 | 1 | 0.0 |
| 299 | 2009-06-28 12:00:00 | 2 | 29895.0 |
| 300 | 2009-07-04 17:00:00 | 2 | 29977.0 |
| 301 | 2009-06-29 16:00:00 | 2 | 29935.0 |
| 302 | 2009-07-11 19:00:00 | 2 | 29991.0 |
| 303 | 2009-07-26 17:00:00 | 2 | 29993.0 |
| 304 | 2009-07-27 00:00:00 | 3 | 30138.0 |
| 305 | 2009-08-04 18:00:00 | 3 | 30200.0 |
| 306 | 2009-08-05 00:00:00 | 3 | 30205.0 |
| 307 | 2009-08-06 00:00:00 | 3 | 30219.0 |
| 308 | 2009-08-15 09:00:00 | 3 | NULL |
| 309 | 2009-08-15 20:00:00 | 2 | 30357.0 |
| 310 | 2009-09-06 12:00:00 | 2 | 30408.0 |
| 311 | 2009-09-07 17:00:00 | 2 | 30453.0 |
| 312 | 2009-09-11 11:00:00 | 2 | 0.0 |
| 313 | 2009-10-25 08:00:00 | 1 | 30531.7 |
| 314 | 2009-10-01 19:00:00 | 1 | 0.0 |
| 317 | 2010-04-07 18:00:00 | 2 | 30571.0 |
| 316 | 2010-03-31 10:00:00 | 1 | 0.0 |
| 318 | 2010-04-18 12:00:00 | 2 | 30640.0 |
| 319 | 2010-04-29 13:00:00 | 1 | 30665.5 |
| 320 | 2010-05-21 16:00:00 | 3 | NULL |
| 321 | 2010-05-22 00:00:00 | 3 | 30752.6 |
| 322 | 2010-05-26 19:00:00 | 1 | 30762.4 |
| 323 | 2010-05-29 18:00:00 | 1 | 30810.8 |
| 344 | 2010-07-19 16:00:00 | 1 | 31062.7 |
| 348 | 2010-08-22 13:00:00 | 2 | NULL |
| 347 | 2010-08-08 09:00:00 | 2 | 31567.0 |
| 346 | 2010-07-25 15:00:00 | 1 | 31485.5 |
| 345 | 2010-07-20 11:00:00 | 1 | 0.0 |
| 343 | 2010-07-21 00:00:00 | 1 | 31382.5 |
| 334 | 2010-06-07 14:00:00 | 2 | 30837.0 |
| 335 | 2010-06-24 18:00:00 | 2 | 30853.0 |
| 336 | 2010-07-01 15:00:00 | 2 | NULL |
| 337 | 2010-07-03 07:00:00 | 2 | 30948.0 |
| 338 | 2010-07-13 12:00:00 | 1 | NULL |
| 339 | 2010-07-13 19:00:00 | 1 | NULL |
| 340 | 2010-07-12 16:00:00 | 1 | NULL |
| 341 | 2010-07-11 21:00:00 | 1 | NULL |
| 349 | 2010-08-22 13:00:00 | 2 | 31682.0 |
| 350 | 2010-09-05 06:00:00 | 2 | 31724.0 |
| 351 | 2010-09-19 14:00:00 | 1 | 31772.4 |
+-----+---------------------+---------+---------+
Now i have to find out which user drove how many km..
i tried it with this
SELECT tt.idusers,sum(tt.next_km - tt.km) as TOTAL_KM
FROM (
SELECT t.idusers,t.km,
(SELECT s.km FROM Reservations s
WHERE s.since > t.since
AND s.km is not null
ORDER BY s.since
LIMIT 1) as next_km
FROM Reservations t) tt
WHERE tt.km is not null
GROUP BY tt.idusers
but then i'll get this output
idusers | TOTAL_KM |
1 | 62831.2
2 | -58077.7
3 | 1223.9
as you see i the query sorts the results for the 'since' so all should be correct and the data is from the odometer when they got the car
but i have problems with the NULLs, they just mess up everything
The query works as follows:
It takes the the km count from the next ID and then calculates minus km from the current ID so it could be NULL(=0)-29945 = -29945 Instead i would like to have it like this: if the next id is equal 0 then the it should take the value from the current km count so that it is like 29945 - 29945 = 0
That would solve the problem, but the next problem is that they are more NULLs in a row.. so it will mess up again
Desired Output should be like:
idUsers | TOTAL_KM
1 | total km from him
2 | total km from him
3 | total km from him
Therefore i ask you if you know a solution to solve this problem or just e query that works..
You can try and check with an IF inside the sum()
SELECT tt.idusers,
sum(
IF(tt.next_km IS NOT NULL AND tt.next_km > 0,tt.next_km - tt.km, tt.km)
) as TOTAL_KM
FROM (
SELECT t.idusers,t.km,
(SELECT s.km FROM Reservations s
WHERE s.since > t.since
AND s.km is not null
ORDER BY s.since
LIMIT 1) as next_km
FROM Reservations t) tt
WHERE tt.km is not null
GROUP BY tt.idusers
This may help you to not get negative result
If tt.next_km is not null and greater than 0 it will return (next_km - km) otherwise it return km, if you want to return 0 when tt-next_km is null then you must change the third expression as shown below to 0
From MySQL Docs
IF(expr1,expr2,expr3)
If I understand correctly, you need the "next" number of km. Then you can use group by:
select r.idusers, sum(r2.next_km - r.km) as total_km
from (select r.*,
(select r2.km
from reservations r2
where r2.since > r.since and r2.km is not null
order by r2.since
limit 1
) as next_km
from reservations r
) r
group by r.idusers;
Hmmm . . . This does look a lot like your query. You should check the data and be sure that the km column is uniformly increasing.
Users are messing . Try
SELECT tt.idusers,sum(tt.next_km - tt.km) as TOTAL_KM
FROM (
SELECT t.idusers,t.km,
(SELECT s.km FROM Reservations s
WHERE s.idusers=t.idusers -- missing part
AND s.since > t.since
AND s.km is not null
ORDER BY s.since
LIMIT 1) as next_km
FROM Reservations t) tt
WHERE tt.km is not null
GROUP BY tt.idusers

SQL Math in SQL statement

I have create a query which solves this problem.
I have a DB with the structure like this
+-----+---------------------+---------+---------+
| id | since | idUsers | km |
+-----+---------------------+---------+---------+
| 124 | 2005-07-18 15:00:00 | 1 | 25798.0 |
| 127 | 2005-07-19 18:00:00 | 3 | 25891.7 |
| 128 | 2005-07-20 00:00:00 | 3 | 25970.2 |
| 129 | 2005-07-18 12:00:00 | 3 | 25795.0 |
| 136 | 2005-07-19 15:00:00 | 1 | 25852.0 |
| 137 | 2005-07-23 12:00:00 | 1 | 26143.6 |
| 139 | 2005-08-07 00:00:00 | 1 | NULL |
| 140 | 2005-08-21 00:00:00 | 1 | NULL |
| 146 | 2005-07-27 15:00:00 | 2 | 26164.0 |
| 147 | 2005-07-28 15:00:00 | 2 | 26178.9 |
| 151 | 2005-08-17 13:00:00 | 1 | 26245.0 |
| 149 | 2005-08-08 18:00:00 | 1 | 26204.9 |
| 150 | 2005-08-10 15:00:00 | 1 | 26221.4 |
| 155 | 2005-08-27 19:00:00 | 1 | NULL |
| 154 | 2005-08-29 09:00:00 | 2 | 26438.3 |
| 156 | 2005-08-28 00:00:00 | 1 | NULL |
| 157 | 2005-08-29 14:00:00 | 3 | 26468.3 |
| 158 | 2005-09-07 15:00:00 | 2 | 26504.2 |
| 159 | 2005-09-05 13:00:00 | 1 | 26478.5 |
| 160 | 2005-09-02 13:00:00 | 1 | 26473.8 |
| 161 | 2005-09-25 12:00:00 | 3 | 26550.4 |
| 162 | 2006-04-02 13:00:00 | 3 | 26599.0 |
| 163 | 2006-04-14 18:00:00 | 1 | NULL |
| 164 | 2006-04-15 00:00:00 | 1 | NULL |
| 169 | 2006-04-16 00:00:00 | 1 | 26703.3 |
| 166 | 2006-04-29 12:00:00 | 1 | NULL |
| 168 | 2006-04-30 00:00:00 | 1 | NULL |
| 170 | 2006-04-16 08:00:00 | 3 | 26709.0 |
| 175 | 2006-05-27 00:00:00 | 3 | NULL |
| 176 | 2006-05-28 00:00:00 | 3 | 27072.0 |
| 177 | 2006-05-26 18:00:00 | 3 | NULL |
| 178 | 2006-04-18 15:00:00 | 2 | 26751.0 |
| 179 | 2006-04-16 17:00:00 | 2 | 26726.0 |
| 181 | 2006-04-23 07:00:00 | 3 | 26775.0 |
| 182 | 2006-05-01 00:00:00 | 1 | 26932.4 |
| 183 | 2006-04-30 18:00:00 | 1 | NULL |
| 184 | 2006-05-11 17:00:00 | 2 | 26988.0 |
| 186 | 2006-06-12 12:00:00 | 2 | 27092.0 |
| 187 | 2006-06-11 19:00:00 | 1 | NULL |
| 188 | 2006-06-12 00:00:00 | 1 | NULL |
| 189 | 2006-06-15 17:00:00 | 2 | 27134.0 |
| 191 | 2006-07-01 11:00:00 | 3 | 27199.0 |
| 192 | 2006-06-23 16:00:00 | 3 | 27162.0 |
| 193 | 2006-07-09 15:00:00 | 2 | 27211.0 |
| 194 | 2006-06-30 18:00:00 | 1 | 27183.0 |
| 195 | 2006-07-26 11:00:00 | 2 | NULL |
| 196 | 2006-08-15 14:00:00 | 2 | 27351.0 |
| 197 | 2006-08-05 16:00:00 | 3 | 27338.0 |
| 198 | 2006-08-06 16:00:00 | 3 | 27341.0 |
| 199 | 2006-09-05 18:00:00 | 3 | NULL |
| 201 | 2006-09-06 00:00:00 | 3 | 27506.0 |
| 202 | 2006-09-16 13:00:00 | 1 | NULL |
| 203 | 2006-09-17 00:00:00 | 1 | NULL |
| 204 | 2006-09-18 00:00:00 | 1 | NULL |
| 205 | 2006-09-19 00:00:00 | 1 | 27568.6 |
| 206 | 2006-10-10 18:00:00 | 1 | 27569.4 |
| 207 | 2006-10-30 15:00:00 | 1 | 27581.5 |
| 209 | 2006-12-29 18:00:00 | 2 | NULL |
| 216 | 2007-04-01 08:00:00 | 1 | 27725.0 |
| 227 | 2007-04-26 00:00:00 | 2 | NULL |
| 214 | 2007-03-29 09:00:00 | 1 | 27675.5 |
| 215 | 2007-03-31 10:00:00 | 1 | 27689.2 |
| 225 | 2007-04-15 08:00:00 | 1 | 27880.3 |
| 224 | 2007-04-14 19:00:00 | 1 | 27800.0 |
| 223 | 2007-04-14 12:00:00 | 1 | 27775.0 |
| 228 | 2007-04-26 23:00:00 | 2 | NULL |
| 229 | 2007-04-27 00:00:00 | 2 | 28000.4 |
| 230 | 2007-04-28 00:00:00 | 2 | NULL |
| 231 | 2007-04-25 12:00:00 | 2 | NULL |
| 232 | 2007-04-20 18:00:00 | 1 | 27906.3 |
| 233 | 2007-04-22 15:00:00 | 1 | 27928.6 |
| 234 | 2007-04-21 17:00:00 | 1 | 27915.2 |
| 236 | 2007-04-23 19:00:00 | 1 | 28068.0 |
| 237 | 2007-05-01 07:00:00 | 3 | 28103.0 |
| 238 | 2007-04-28 18:00:00 | 1 | 28030.3 |
| 239 | 2007-05-20 11:00:00 | 1 | 28174.2 |
| 240 | 2007-05-20 15:00:00 | 2 | 28181.0 |
| 241 | 2007-05-23 16:00:00 | 2 | 28198.0 |
| 242 | 2007-06-03 16:00:00 | 2 | 28238.0 |
| 243 | 2007-06-10 09:00:00 | 3 | NULL |
| 244 | 2007-07-01 12:00:00 | 2 | 28291.0 |
| 245 | 2007-07-07 15:00:00 | 2 | 28343.0 |
| 246 | 2007-07-30 15:00:00 | 1 | 28354.8 |
| 247 | 2007-07-31 12:00:00 | 1 | 28476.9 |
| 248 | 2007-08-01 15:00:00 | 1 | NULL |
| 249 | 2007-08-04 11:00:00 | 3 | NULL |
| 250 | 2007-08-05 00:00:00 | 3 | NULL |
| 251 | 2007-08-05 10:00:00 | 3 | NULL |
| 252 | 2007-08-06 00:00:00 | 3 | 28708.0 |
| 253 | 2007-08-09 17:00:00 | 1 | 28729.9 |
| 254 | 2007-08-24 19:00:00 | 2 | 28806.0 |
| 255 | 2007-09-01 10:00:00 | 2 | 28812.0 |
| 256 | 2007-09-16 13:00:00 | 3 | 28842.0 |
| 257 | 2007-10-16 02:00:00 | 2 | NULL |
| 258 | 2008-06-19 12:00:00 | 1 | NULL |
| 259 | 2008-06-20 00:00:00 | 1 | NULL |
| 260 | 2008-06-21 00:00:00 | 1 | NULL |
| 261 | 2008-06-22 00:00:00 | 1 | NULL |
| 262 | 2008-06-23 00:00:00 | 1 | NULL |
| 263 | 2008-06-24 00:00:00 | 1 | 28986.7 |
| 264 | 2008-06-26 18:00:00 | 2 | 28997.0 |
| 265 | 2008-06-29 13:00:00 | 2 | 28998.0 |
| 266 | 2008-06-29 18:00:00 | 3 | 29012.0 |
| 267 | 2008-07-08 16:00:00 | 2 | 29019.0 |
| 268 | 2008-07-28 18:00:00 | 2 | 29082.0 |
| 269 | 2008-08-10 15:00:00 | 2 | 29192.0 |
| 270 | 2008-08-21 17:00:00 | 2 | NULL |
| 271 | 2008-08-24 13:00:00 | 2 | 29281.0 |
| 272 | 2008-08-27 21:00:00 | 3 | NULL |
| 273 | 2008-08-28 00:00:00 | 3 | NULL |
| 274 | 2008-08-29 00:00:00 | 3 | NULL |
| 275 | 2008-08-30 00:00:00 | 3 | 29343.0 |
| 276 | 2008-08-30 18:00:00 | 2 | 29352.0 |
| 277 | 2008-09-05 17:00:00 | 2 | 29385.0 |
| 279 | 2008-10-10 14:00:00 | 1 | NULL |
| 280 | 2008-10-11 00:00:00 | 1 | NULL |
| 281 | 2008-10-12 00:00:00 | 1 | NULL |
| 282 | 2008-10-13 00:00:00 | 1 | 29459.0 |
| 283 | 2009-04-05 10:00:00 | 2 | 29460.0 |
| 286 | 2009-04-19 10:00:00 | 3 | 29471.0 |
| 285 | 2009-05-02 00:00:00 | 1 | NULL |
| 287 | 2009-04-22 15:00:00 | 2 | 29486.0 |
| 288 | 2009-05-01 10:00:00 | 1 | NULL |
| 290 | 2009-05-20 15:00:00 | 3 | NULL |
| 289 | 2009-05-03 00:00:00 | 1 | 29668.6 |
| 291 | 2009-05-21 00:00:00 | 3 | NULL |
| 292 | 2009-05-22 00:00:00 | 3 | NULL |
| 293 | 2009-05-23 00:00:00 | 3 | NULL |
| 294 | 2009-05-22 09:00:00 | 3 | NULL |
| 295 | 2009-05-24 00:00:00 | 3 | 29820.0 |
| 296 | 2009-05-31 15:00:00 | 2 | 29830.0 |
| 297 | 2009-06-07 16:00:00 | 1 | 29850.3 |
| 298 | 2009-07-05 00:00:00 | 1 | 0.0 |
| 299 | 2009-06-28 12:00:00 | 2 | 29895.0 |
| 300 | 2009-07-04 17:00:00 | 2 | 29977.0 |
| 301 | 2009-06-29 16:00:00 | 2 | 29935.0 |
| 302 | 2009-07-11 19:00:00 | 2 | 29991.0 |
| 303 | 2009-07-26 17:00:00 | 2 | 29993.0 |
| 304 | 2009-07-27 00:00:00 | 3 | 30138.0 |
| 305 | 2009-08-04 18:00:00 | 3 | 30200.0 |
| 306 | 2009-08-05 00:00:00 | 3 | 30205.0 |
| 307 | 2009-08-06 00:00:00 | 3 | 30219.0 |
| 308 | 2009-08-15 09:00:00 | 3 | NULL |
| 309 | 2009-08-15 20:00:00 | 2 | 30357.0 |
| 310 | 2009-09-06 12:00:00 | 2 | 30408.0 |
| 311 | 2009-09-07 17:00:00 | 2 | 30453.0 |
| 312 | 2009-09-11 11:00:00 | 2 | 0.0 |
| 313 | 2009-10-25 08:00:00 | 1 | 30531.7 |
| 314 | 2009-10-01 19:00:00 | 1 | 0.0 |
| 317 | 2010-04-07 18:00:00 | 2 | 30571.0 |
| 316 | 2010-03-31 10:00:00 | 1 | 0.0 |
| 318 | 2010-04-18 12:00:00 | 2 | 30640.0 |
| 319 | 2010-04-29 13:00:00 | 1 | 30665.5 |
| 320 | 2010-05-21 16:00:00 | 3 | NULL |
| 321 | 2010-05-22 00:00:00 | 3 | 30752.6 |
| 322 | 2010-05-26 19:00:00 | 1 | 30762.4 |
| 323 | 2010-05-29 18:00:00 | 1 | 30810.8 |
| 344 | 2010-07-19 16:00:00 | 1 | 31062.7 |
| 348 | 2010-08-22 13:00:00 | 2 | NULL |
| 347 | 2010-08-08 09:00:00 | 2 | 31567.0 |
| 346 | 2010-07-25 15:00:00 | 1 | 31485.5 |
| 345 | 2010-07-20 11:00:00 | 1 | 0.0 |
| 343 | 2010-07-21 00:00:00 | 1 | 31382.5 |
| 334 | 2010-06-07 14:00:00 | 2 | 30837.0 |
| 335 | 2010-06-24 18:00:00 | 2 | 30853.0 |
| 336 | 2010-07-01 15:00:00 | 2 | NULL |
| 337 | 2010-07-03 07:00:00 | 2 | 30948.0 |
| 338 | 2010-07-13 12:00:00 | 1 | NULL |
| 339 | 2010-07-13 19:00:00 | 1 | NULL |
| 340 | 2010-07-12 16:00:00 | 1 | NULL |
| 341 | 2010-07-11 21:00:00 | 1 | NULL |
| 349 | 2010-08-22 13:00:00 | 2 | 31682.0 |
| 350 | 2010-09-05 06:00:00 | 2 | 31724.0 |
| 351 | 2010-09-19 14:00:00 | 1 | 31772.4 |
+-----+---------------------+---------+---------+
Now i have to find out which user drove how many km..
it could be easy with
Select r.idUsers, SUM(r.km) AS KM_TOTAL FROM Reservations r GROUP BY r.idUsers
but the problem is the value in the km field is the value of the car right now, so basically i have to find out the difference between the id from the next row and the current row (id127.km - id124.km)
but i just started programming and i dont know how to solve this
The end results need be shown like this:
idUsers | TOTAL_KM
1 | (Total value of his km)
2 | (Total value of his km)
3 | (Total value of his km)
I hope you can help me
EDIT:
SELECT tt.iduser,sum(tt.next_km - tt.km) as TOTAL_KM
FROM (
SELECT t.iduser,t.km,
(SELECT s.km FROM Reservations s
WHERE s.since > t.since
ORDER BY s.since
LIMIT 1) as next_km
FROM Reservations t) tt
WHERE tt.next_km is not null
GROUP BY tt.iduser
Gives the output
idusers | TOTAL_KM
1 | 62141.9
2 | -59284.9
3 | 596.9
You can do it using a correlated query to fetch the next KM , and them sum the difference between them for each user :
SELECT tt.iduser,sum(tt.next_km - tt.km) as TOTAL_KM
FROM (
SELECT t.iduser,t.km,
(SELECT s.km FROM Reservations s
WHERE s.since > t.since
ORDER BY s.since
LIMIT 1) as next_km
FROM Reservations t) tt
WHERE tt.next_km is not null
AND tt.km is not null
GROUP BY tt.iduser
You didn't tag your RDBMS, if its SQL-Server use to TOP 1 , if its MySQL use the LIMIT 1 , if its something else, then tell me and I'll adjust it.
SELECT r.iduser,max(r.km)-min(r.km) as TOTAL_KM
FROM Reservations r
WHERE r.km is not null
GROUP BY r.iduser

SQL less than 7 days

i need to make a selection of all the values of the same order_ID, where the difference between the first date and the last is equal or less than 7, but I can't figure out how I should do this, could any one give me some help how I should do this?
DB layout:
+---------------+---------------------+
| order_ID | date |
+---------------+---------------------+
| 1 | 2012-01-21 09:15:00 |
| 1 | 2012-01-21 09:15:00 |
| 2 | 2012-01-21 14:15:00 |
| 3 | 2012-01-22 10:05:00 |
| 3 | 2012-01-22 10:05:00 |
| 4 | 2012-01-22 14:25:00 |
| 5 | 2012-01-25 11:05:00 |
| 6 | 2012-01-27 16:10:00 |
| 6 | 2012-01-27 16:10:00 |
| 7 | 2012-01-27 16:10:00 |
| 8 | 2012-01-28 15:55:00 |
| 9 | 2012-01-28 16:40:00 |
| 10 | 2012-01-28 17:35:00 |
| 10 | 2012-01-28 17:35:00 |
| 10 | 2012-02-03 09:35:00 |
| 12 | 2012-02-03 10:15:00 |
| 13 | 2012-02-05 13:25:00 |
| 14 | 2012-02-05 13:45:00 |
| 15 | 2012-02-05 14:10:00 |
| 15 | 2012-02-05 14:10:00 |
| 16 | 2012-02-07 15:45:00 |
| 17 | 2012-02-11 11:50:00 |
| 18 | 2012-02-11 12:45:00 |
| 19 | 2012-02-14 09:10:00 |
| 19 | 2012-02-16 13:05:00 |
| 19 | 2012-02-16 13:05:00 |
| 20 | 2012-02-17 09:40:00 |
| 21 | 2012-02-19 14:10:00 |
| 21 | NULL |
| 22 | 2012-02-25 10:20:00 |
| 23 | 2012-02-25 11:05:00 |
| 24 | 2012-02-25 13:35:00 |
| 25 | 2012-02-27 17:10:00 |
| 26 | 2012-03-03 09:40:00 |
| 27 | 2012-03-05 10:45:00 |
| 28 | NULL |
| 29 | NULL |
| 29 | NULL |
| 29 | NULL |
| 30 | NULL |
| 30 | NULL |
+---------------+---------------------+
You'll need to decide how you want to handle nulls (coalesce to the maximum or minimum date value?), but this should give you a basic structure for your query.
select order_id
from (
select order_id, min(date) as mindate, max(date) as maxdate
from tbl
group by order_id) t
where datediff(maxdate,mindate) <= 7