SQL Query handling NULLs - mysql

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

Related

I want to retrieve which scheduler is scheduled for a particular day

+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| ID | Name | Action | Created By Use Rid | Created Date | Days | Enterprise ID | Job ID | Last Updated By Use Rid | Last Updated Date | Relays | Sch Time | Status | Timezone | Trigger ID | User ID |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
| 30 | ios | 1 | 2 | 1559739565592 | | 2 | ef29baba-534d-4151-80d3-f5a589cf7c22 | 2 | 1559739565592 | 74 | 2019-06-05T13:04:00.000Z | 2 | Asia/Kolkata | 0492dc45-6671-4064-baef-8654761b67f2 | 2 |
| 31 | iot | 0 | 2 | 1559739617863 | 1,2,3,4,5,6,7 | 2 | 85e4a611-37dc-45d3-8a2f-243e6d4e0962 | 2 | 1575869484168 | 508,509,504,505,506,507 | 2019-12-09T13:50:00.000Z | 0 | Asia/Kolkata | b8a71ee4-94bd-4d2b-b875-7ac1468fad11 | 2 |
| 32 | Daily-8:50 Pm | 1 | 2 | 1559747777019 | | 2 | 3f87f532-6289-4016-a6d4-2a76baf4ffb5 | 2 | 1559749199040 | 45,46,47,48,50,51,52,61,62,63,64,65,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60,49,66,67,68,304,305 | 2019-06-05T15:41:00.000Z | 2 | Asia/Kolkata | 6f1b873b-1e96-47bd-b723-4f50d5534f89 | 2 |
| 33 | TVOffSchedular | 0 | 2 | 1559933346322 | 1,2,3,4,5,6,7 | 4 | 1935f0cf-cdf9-4bdf-895d-9e6dc6c4ddff | 2 | 1560453392696 | 321,325 | 2019-06-13T19:20:00.000Z | 0 | Asia/Kolkata | c6bf383d-7bd7-4364-b077-e8bb3ad857f7 | 2 |
| 34 | TVOnMorning | 1 | 2 | 1559933392595 | 1,2,3,4,5,6,7 | 4 | d90d5c17-46b9-46cc-bb33-07557bf97684 | 2 | 1559933392595 | 321,325 | 1970-01-01T00:00:00.000Z | 2 | Asia/Kolkata | a131188d-aa7d-4132-995e-035e172566a7 | 2 |
| 35 | MorningOfficeON | 1 | 2 | 1560004379991 | 1,2,3,4,5,6 | 2 | 8b9163e7-bdea-4ee3-b827-3ab3d4728a0c | 2 | 1561881126773 | 49 | 2019-06-08T02:30:00.000Z | 0 | Asia/Kolkata | 926e809f-b1fb-4d3c-8da7-b048c0366a3a | 2 |
| 36 | EveningOfficeOFF | 0 | 2 | 1560005726195 | 1,2,3,4,5,6 | 2 | a7161d66-9ae4-4eb9-bc6e-ded6bd01a766 | 2 | 1560261033555 | 49 | 2019-06-11T14:00:00.000Z | 0 | Asia/Kolkata | dc45718f-aad4-4eb2-9b94-8a8bc4444385 | 2 |
| 37 | Good night | 1 | 3 | 1560088585521 | 1,2,3,4,5,6,7 | 3 | 4e8eedd1-956d-4922-9715-8f9b7bf221cb | 3 | 1560088585521 | 253,363 | 1970-01-01T14:00:00.000Z | 2 | Asia/Kolkata | 39a8fb76-06d6-4fdd-8c1f-827c3c1e2c24 | 3 |
| 38 | Good night off | 0 | 3 | 1560088719173 | 1,2,3,4,5,6,7 | 3 | c0a9dfdd-7b1b-4a9e-a9f1-b31d927d995e | 3 | 1560088719173 | 253,363 | 1969-12-31T19:30:00.000Z | 2 | Asia/Kolkata | 61cfacdd-f2c8-46c9-b192-228aef33cc59 | 3 |
| 39 | ApplicationON | 1 | 2 | 1560140790669 | 1,2,3,4,5,6,7 | 2 | 8f602cc4-8fe7-416a-b563-9b25cd008e5b | 2 | 1560141163056 | 45,46,47,48,50,51,52 | 2019-06-10T04:35:00.000Z | 0 | Asia/Kolkata | 5bab6ae0-7c7b-487c-9e72-d6200e0985a2 | 2 |
| 40 | ApplicationMorningOFF | 0 | 2 | 1560140849363 | 1,2,3,4,5,6,7 | 2 | 7d34c4f1-3638-4355-a14c-622bf733e23e | 2 | 1560141176600 | 45,46,47,48,50,51,52 | 2019-06-10T04:40:00.000Z | 0 | Asia/Kolkata | 4cd78892-c2b2-493e-a839-b270eff5aea5 | 2 |
| 41 | DesignNoonOFF | 0 | 2 | 1560241956756 | 1,2,3,4,5,6,7 | 2 | 00d3437b-a062-4f32-bb37-200574cac235 | 2 | 1567674559401 | 512,513,514,515 | 2019-09-05T08:15:00.000Z | 0 | Asia/Kolkata | b10d1ded-60a3-4adf-ba6c-743ec3dc4f29 | 2 |
| 42 | DesignNoonON | 1 | 2 | 1560241995428 | 1,2,3,4,5,6,7 | 2 | f3e3807c-7fad-4b08-bb18-439222f8d454 | 2 | 1567674587127 | 512,513,514,515 | 2019-09-05T08:30:00.000Z | 0 | Asia/Kolkata | c53e9069-164e-430a-89be-913c8127a767 | 2 |
| 43 | blockchain-test | 1 | 2 | 1560255684868 | 1,2,3,4,5,6,7 | 2 | 4ac65798-682e-447d-b6cf-813308f05efe | 2 | 1560257490068 | 45,46,47,48,50,51,52,61,62,63,64,65,302,73,74,75,76,53,54,57,58,59,60 | 2019-06-11T12:52:00.000Z | 2 | Asia/Calcutta | 8e7640ea-0223-4a9b-abdc-73c139f8e6fa | 2 |
| 44 | NightLampON | 1 | 2 | 1560360082007 | 1,2,3,4,5,6,7 | 4 | b5aee8d5-8013-4303-88df-6a5e3bc7330e | 2 | 1560360082007 | 322 | 1970-01-01T17:25:00.000Z | 0 | Asia/Kolkata | d0f3a1b5-93f3-4858-be5f-4837e62dd9bf | 2 |
| 45 | blockchain-off | 1 | 2 | 1560432958716 | 1,2,3,4,5,6,7 | 2 | a3d89bb2-3710-4f8e-9814-1e6942d1a405 | 2 | 1560432958716 | 53,54,57,59,60 | 2019-06-13T13:45:00.000Z | 0 | Asia/Calcutta | a0ccdcb6-a55e-48e2-9391-60d62e10fe4a | 2 |
| 46 | AllLightOffExceptOfficeMaster | 0 | 2 | 1560524588879 | | 2 | 64939311-522e-4877-b53f-e44ff78200b7 | 2 | 1560524588879 | 45,46,47,48,50,51,52,61,62,63,64,302,303,73,74,75,76,77,53,54,55,56,57,58,59,60 | 2019-06-14T15:10:00.000Z | 0 | Asia/Kolkata | 4a799d93-877c-4d19-b19a-40b6b9767564 | 2 |
| 47 | NightLampOFF | 0 | 2 | 1560533195543 | 1,2,3,4,5,6,7 | 4 | c844b235-3851-4692-ab14-4ee31b5d9404 | 2 | 1560620178500 | 322 | 2019-06-15T17:40:00.000Z | 0 | Asia/Kolkata | a48c0a99-76cd-4268-b946-8d63408aa234 | 2 |
| 48 | TVON | 1 | 2 | 1561124197968 | 1,2,3,4,5,6,7 | 4 | b0617f91-54b2-48bc-9cf9-2f42fb1526c4 | 2 | 1571035693157 | 321,325 | 2019-06-20T23:30:00.000Z | 0 | Asia/Kolkata | 93d07355-1c95-4faa-ac5c-73b0c68b5323 | 2 |
| 49 | panel light/Inside logo on | 1 | 3 | 1561492261954 | 1,2,3,4,5,6 | 3 | 736609ff-6391-474a-9556-85eff54f2fd8 | 3 | 1561492261954 | 359,360,361,364 | 2019-06-26T14:00:00.000Z | 2 | Asia/Kolkata | 84de41a7-a141-49d3-8bf2-8a9a229a8c56 | 3 |
| 50 | Outside Board/panel/focus light ON | 1 | 3 | 1562096267147 | 1,2,3,4,5,6,7 | 3 | 37d4f238-15b3-4a1a-bac1-a050d61792f7 | 3 | 1572442799792 | 894 | 1970-01-01T13:45:00.000Z | 2 | Asia/Kolkata | 3610c4b9-6180-4640-a078-3e310d80b4dd | 3 |
| 51 | Outside Board/Panel/Focus light OFF | 0 | 3 | 1562096428205 | 1,2,3,4,5,6,7 | 3 | 7ef0d450-39f9-4449-beec-c46416c3ebb2 | 3 | 1572442815806 | 894 | 2019-10-30T16:30:00.000Z | 2 | Asia/Kolkata | 8d9fe67d-7d39-4edb-8191-ea8af176b5d7 | 3 |
| 52 | Inside Logo/Panel/Display/Ganpati/Entry light ON | 1 | 3 | 1562096734383 | 1,2,3,4,5,6 | 3 | 4b6bab36-889a-45ca-969c-a1cecea1f42f | 3 | 1562096734383 | 110,111,359,360,361,364 | 2019-07-03T13:30:00.000Z | 2 | Asia/Kolkata | c08cc35a-8e64-4d17-a673-4bfddb2f1c92 | 3 |
| 53 | Inside Logo/Panel/Display/Ganpati/Entrance OFF | 0 | 3 | 1562097007454 | 1,2,3,4,5,6 | 3 | 54de42a4-f28e-494a-9ec3-56e22e7956e9 | 3 | 1562257875176 | 110,111,359,360,361,364 | 2019-07-03T16:30:00.000Z | 2 | Asia/Kolkata | ffbf0fcd-e6ae-41ba-8873-0d2bbd5d3cd0 | 3 |
| 54 | EveningOfficeON | 1 | 2 | 1562852792955 | | 2 | b4c3b59f-8157-4260-b0c0-a76b739e857b | 2 | 1562852792955 | 45,46,47,48,61,62,63,64,49 | 2019-07-11T14:01:00.000Z | 0 | Asia/Kolkata | 4be1a145-8e21-405e-990d-f85d0c3657bf | 2 |
| 55 | WaterCoolerON | 1 | 2 | 1564217226090 | 1,2,3,4,5,6,7 | 2 | 1e9c429f-9a51-4147-815b-efad95330baa | 2 | 1564217226090 | 489 | 1970-01-01T00:30:00.000Z | 0 | Asia/Kolkata | 4b9413ae-6d13-4117-bc8d-36f02c462b05 | 2 |
| 56 | WaterCoolerOff | 0 | 2 | 1564217269330 | 1,2,3,4,5,6,7 | 2 | 0f2c1112-dd26-43eb-8390-a1bba53789de | 2 | 1564217269330 | 489 | 1970-01-01T14:30:00.000Z | 0 | Asia/Kolkata | 907e4ee5-367a-4b4b-bc6b-716eeed4c8b8 | 2 |
| 57 | Test 1 | 1 | 28 | 1567236412976 | | 6 | 2c8276b3-a946-475c-a0e9-7e55569cadae | 28 | 1567236412976 | 553 | 2019-08-31T07:28:00.000Z | 2 | IST | dc1fec52-8c98-4832-b01a-a3a91f0b6058 | 28 |
| 58 | Test 3 | 1 | 28 | 1567236499938 | | 6 | fc820b65-a04c-4ecf-9876-2bd4bb8a8816 | 28 | 1567237094910 | 556,557 | 2019-08-31T07:39:00.000Z | 2 | IST | ceca92e5-765c-4f54-97ba-77447147276f | 28 |
| 59 | Test 1.1 | 1 | 28 | 1567236608884 | | 6 | 985e07c0-3a53-4884-a169-19e8a9ae630b | 28 | 1567236608884 | 553 | 2019-08-31T07:32:00.000Z | 2 | IST | f1a18b94-ef35-46a7-afbe-c75e743185fe | 28 |
| 60 | Outside C&C Ledlight On | 1 | 3 | 1570365557312 | 1,2,3,4,5,6,7 | 3 | ad17d61e-549e-44f1-99db-b50105d069b3 | 3 | 1574947645835 | 893 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 82b0fc02-c98c-4b12-9d8a-3ba2897d1aa7 | 3 |
| 61 | test | 0 | 2 | 1570532481972 | 1 | 2 | 2d058c32-89f8-4709-ba77-cca2c677334d | 2 | 1570532481972 | 524,525,526,527,529,530,531 | 2019-10-08T11:00:00.000Z | 2 | Africa/Lusaka | 722aece6-39d5-44be-bace-967180cd449f | 2 |
| 62 | Outside C&C Ledlight Off | 0 | 3 | 1570647085459 | 1,2,3,4,5,6,7 | 3 | 24c4831d-3491-4fa9-a8e2-dab21a94aa93 | 3 | 1572636959040 | 893 | 2019-10-29T19:00:00.000Z | 0 | Asia/Kolkata | 41c21970-faf8-4699-9a97-a49e1b28f3ce | 3 |
| 63 | bedroom allout 1 | 1 | 2 | 1571346220354 | 1,2,3,4,5,6,7 | 7 | 9ba4ec5b-c645-4cf6-bede-59fa79073134 | 2 | 1574015422525 | 662 | 1970-01-01T17:03:00.000Z | 0 | Asia/Calcutta | 4aff33ae-6af6-45f3-8407-f67a6f344e4b | 2 |
| 64 | bedroom allout off | 0 | 2 | 1571346263753 | 1,2,3,4,5,6,7 | 7 | 8cd3c6ac-0ea6-49b9-af09-10e9dcb3847b | 2 | 1571346263753 | 662 | 1970-01-01T01:04:00.000Z | 0 | Asia/Calcutta | 4ecfcf3f-110c-44b2-9a8d-3f35f1d5a295 | 2 |
| 65 | PIR Enable | 1 | 25 | 1571649294987 | 1,2,3,4,5,6,7 | 9 | c142b8f1-77d6-46a2-8f45-46ad3770a3c4 | 25 | 1571661763643 | 756 | 2019-10-21T13:00:00.000Z | 2 | Asia/Kolkata | 05fc4108-f65a-449f-a211-10e76e080196 | 25 |
| 66 | PIR Disable | 0 | 25 | 1571649351212 | 1,2,3,4,5,6,7 | 9 | 894ef781-cb8f-4139-907f-5d20fc5a0861 | 25 | 1571649351212 | 756 | 2019-10-21T01:30:00.000Z | 2 | Asia/Kolkata | 0f927aca-f592-44bb-9a7f-67544aa32223 | 25 |
| 67 | testing 22 | 1 | 2 | 1571734869588 | | 7 | 2fe74039-d25e-4384-bc4a-dd63843de063 | 2 | 1574015556036 | 639 | 2019-11-20T09:03:00.000Z | 0 | Asia/Calcutta | 86cfd858-367a-4a0b-b7eb-35e1096b56f4 | 2 |
| 68 | mobile charger | 0 | 23 | 1572199190164 | 1,2,3,4,5,6,7 | 11 | 7d536ef8-d24a-4a53-94c0-6c52a441312a | 23 | 1575485933026 | 773,774 | 2019-12-04T21:00:00.000Z | 0 | Asia/Calcutta | fc8b10b3-43a5-497c-86b9-bd55a80cfe4a | 23 |
| 69 | Outside Focus light On | 1 | 3 | 1572637078856 | 1,2,3,4,5,6,7 | 3 | 9a889394-8102-42a0-836a-43a7ffb5c7bf | 3 | 1572983697418 | 922 | 2019-11-06T12:30:00.000Z | 0 | Asia/Kolkata | 4747ca5e-736b-4b80-b7a4-9abfe33f0e77 | 3 |
| 70 | Outside Focus light Off | 0 | 3 | 1572637169312 | 1,2,3,4,5,6,7 | 3 | 1a283075-37e6-4152-a716-a8b63ef62f56 | 3 | 1572637169312 | 922 | 1969-12-31T18:35:00.000Z | 0 | Asia/Kolkata | fa79f7f6-bde4-4d87-96fd-b570ad47a57c | 3 |
| 71 | Ganpati/Display On | 1 | 3 | 1572637346879 | 2,3,4,5,6,7 | 3 | 6ae8c9ca-e26b-48b8-a3be-ef2d41b0a0d6 | 3 | 1574943671391 | 924,926,894 | 2019-11-28T12:30:00.000Z | 0 | Asia/Kolkata | 0027b2eb-efa2-41ff-ac24-50483eec4632 | 3 |
| 72 | Ganpati/Display Off | 0 | 3 | 1572637442853 | 2,3,4,5,6,7 | 3 | 0bb73aca-c82d-4459-922b-cb3592783ae9 | 3 | 1574943713949 | 924,926,894 | 2019-11-28T16:30:00.000Z | 0 | Asia/Kolkata | f8fb596e-dd42-4227-98cb-03a8dcbee9bb | 3 |
| 73 | panel/Logo/Table logo/ Light On | 1 | 3 | 1572638062814 | 2,3,4,5,6,7 | 3 | ab78a500-af55-4c8c-84a8-fdbbe76404f4 | 3 | 1572638089623 | 853,855 | 2019-11-02T12:30:00.000Z | 0 | Asia/Kolkata | 0ef71b02-744c-4755-b7dc-bc51dd727fe9 | 3 |
| 74 | all-out-off | 0 | 13 | 1572887848318 | 2,3,4,5,6 | 10 | 213f3bab-332a-4992-9d2d-7e38cf48dbfd | 13 | 1572887848318 | 838 | 2019-11-04T01:30:00.000Z | 0 | Asia/Calcutta | 70938fed-0053-443e-b302-e0f8e25826bb | 13 |
| 75 | mbroom-fan-off | 0 | 13 | 1572888838651 | 1,2,3,4,5,6,7 | 10 | ae1b74c2-2ad7-4182-9b4d-3f15812edbe8 | 13 | 1572974583661 | 834 | 2019-11-04T20:45:57.000Z | 0 | Asia/Calcutta | e87e19e4-d8ae-45b4-b575-1110d1368648 | 13 |
| 76 | PIR Enable | 1 | 25 | 1572944519175 | 1,2,3,4,5,6,7 | 9 | 6fc17694-4fc5-443e-92ee-ed1132a2ec35 | 25 | 1572944519175 | 758 | 2019-11-05T12:46:00.000Z | 0 | Asia/Kolkata | 46ea68cd-7e11-4003-bdfb-54aab6ba3923 | 25 |
| 77 | PIR Disable | 0 | 25 | 1572944690715 | 1,2,3,4,5,6,7 | 9 | ae7a333c-ff75-4c8b-bcae-92b308180b93 | 25 | 1573197064859 | 758 | 2019-11-06T00:20:00.000Z | 0 | Asia/Kolkata | 6a41e2f1-d029-4d28-9ce7-b2adebb59dab | 25 |
| 78 | test 1 | 1 | 2 | 1573129815561 | | 16 | ab60b929-48ed-43ab-9a89-45bfc0a10ee3 | 2 | 1573129815561 | 1030 | 2019-11-07T12:32:13.000Z | 0 | Asia/Kolkata | 80c7d1b2-68b4-425e-91b4-d82544d66dfc | 2 |
| 79 | pir en test | 1 | 25 | 1573196361376 | 1,2,3,4,5,6,7 | 9 | a206b1f6-f1e8-4c12-a97f-b7d217fd344a | 25 | 1573196739778 | 758 | 2019-11-08T07:08:00.000Z | 2 | Asia/Kolkata | dc23b8ee-2da0-42a3-a6d0-35a328035e12 | 25 |
| 80 | pir dbtest | 0 | 25 | 1573196399643 | 1,2,3,4,5,6,7 | 9 | 5d133d17-8495-4e34-aa6c-ea13344ddfdc | 25 | 1573196765147 | 758 | 2019-11-08T07:09:00.000Z | 2 | Asia/Kolkata | b431f3fc-f541-4912-b698-83f574c1c811 | 25 |
| 81 | living-off | 0 | 13 | 1573408027950 | | 10 | cac70a87-98b3-405f-a6da-2ac6f1c3a344 | 13 | 1573408137563 | 813 | 2019-11-10T20:30:56.000Z | 0 | Asia/Calcutta | 7b91172a-da7b-4f94-bf92-7915b51fce98 | 13 |
| 82 | ro on | 1 | 2 | 1573416862256 | 1,2,3,4,5,6,7 | 7 | 22e7e1ab-d2f3-483e-bcc5-3aa595feedb8 | 2 | 1573416862256 | 672 | 1970-01-01T00:08:00.000Z | 0 | Asia/Calcutta | 747f6396-77b7-4bab-93a2-6c6715b3e87f | 2 |
| 83 | RO off | 0 | 2 | 1573473725301 | 1,2,3,4,5,6,7 | 7 | eed1acbd-0850-4dc1-ad65-2528eb630389 | 2 | 1573473725301 | 672 | 1969-12-31T19:01:00.000Z | 0 | Asia/Calcutta | 89a511e2-52f6-45f3-9257-78b7f9f41bc2 | 2 |
| 84 | Test | 1 | 42 | 1573738374195 | | 17 | fca106ba-af83-4bae-8855-aa4d9edd4484 | 42 | 1573738590203 | 1133,1134,1135,1136 | 2019-11-14T13:38:29.000Z | 0 | Asia/Kolkata | f668dc34-db5b-4f34-a5a1-ee7649bc093d | 42 |
| 85 | entrance | 0 | 42 | 1573738946510 | | 17 | ce332a00-b3ca-45cc-9c1d-c3ead60845fd | 42 | 1573738946510 | 1133 | 2019-11-14T13:47:25.000Z | 0 | Asia/Kolkata | c7b912db-096a-4836-8b1d-0b52197922c5 | 42 |
| 86 | ronak bedroom charger | 1 | 15 | 1573827203905 | 1,2,3,4,5,6,7 | 15 | a31cc0a3-cfcd-4a57-8a72-f292893c4de9 | 15 | 1574274253149 | 1103 | 2019-11-14T23:30:00.000Z | 0 | Asia/Kolkata | 91a3e0c7-f750-42ca-84e0-af4bbb7ef839 | 15 |
| 87 | ronak bedroom plug off | 0 | 15 | 1573827259380 | 1,2,3,4,5,6,7 | 15 | 7f9e1f0f-e126-4bdd-acce-967bfb785c25 | 15 | 1574274268343 | 1103 | 2019-11-15T01:30:00.000Z | 0 | Asia/Kolkata | dc3b7742-711c-46c5-9395-38c9568d32dc | 15 |
| 88 | fan off | 0 | 15 | 1573845123332 | 1,2,3,4,5,6,7 | 15 | d5c68535-499a-436d-844f-8d0d7140d2da | 15 | 1573845123332 | 1096 | 2019-11-15T20:32:00.000Z | 0 | Asia/Kolkata | a014d04c-48dc-4ec4-a56f-90383387c241 | 15 |
| 89 | master beroom all out on | 1 | 20 | 1574014827858 | 1,2,3,4,5,6,7 | 8 | f25ae2d2-9414-418a-878b-1545a956d5fe | 20 | 1574014827858 | 700 | 2019-11-17T17:00:00.000Z | 0 | Asia/Kolkata | 3cded4f6-9740-43fa-b921-b5b9deb5bdb5 | 20 |
| 90 | master bedroom plug 4 | 0 | 20 | 1574015013470 | 1,2,3,4,5,6,7 | 8 | 1daabfe0-ccaa-4821-8658-c556541771a4 | 20 | 1574042395372 | 700 | 2019-11-18T00:30:00.000Z | 0 | Asia/Kolkata | fa0aad18-fe38-4e2f-8753-caef702ab74c | 20 |
| 91 | light on | 0 | 22 |
+-----+--------------------------------------------------+--------+--------------------+---------------+---------------+---------------+--------------------------------------+-------------------------+-------------------+--------------------------------------------------------------------------------------------------------+--------------------------+--------+----------------+--------------------------------------+---------+
In the below data the sch time needs to be compared with Days ie when the Days column is null then sch Time should be the output and when it is 1,2,3,4,5,6,7 then case when 1 then return sunday, case when 2 then return monday,case when 3 then return tues, case when 4 then return wed, case when 5 then return thur, case when 6 then return fri, case when 7 then return sat. should be the output The data should be time and date sorted decreasing
Please check the below query and put your table name at the place Your_table.
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0) --You can add this in current table or temp table
WHILE EXISTS (
SELECT TOP 1 update_stat
FROM Your_Table
WHERE update_stat = 0
ORDER BY Id
)
BEGIN
DECLARE #days VARCHAR(100)
DECLARE #datepart VARCHAR(10)
DECLARE #Id INT
SET #datepart = DATEPART(dw, getdate())
SET #days = (
SELECT TOP 1 days
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
SET #ID = (
SELECT TOP 1 ID
FROM dbo.dates
WHERE days IS NOT NULL
ORDER BY ID
)
IF CHARINDEX(#datepart, #days) > 0
BEGIN
UPDATE Your_Table
SET update_stat = 1
WHERE id = #Id
END
SELECT
SELECT DATEPART(d, getdate()) AS DaysNo
,CASE DATEPART(WEEKDAY, GETDATE())
WHEN 1
THEN 'SUNDAY'
WHEN 2
THEN 'MONDAY'
WHEN 3
THEN 'TUESDAY'
WHEN 4
THEN 'WEDNESDAY'
WHEN 5
THEN 'THURSDAY'
WHEN 6
THEN 'FRIDAY'
WHEN 7
THEN 'SATURDAY'
END AS DaysName
,*
FROM Your_Table
WHERE update_stat = 1
You can insert the new column the update_stat either in the same table or you can insert all the existing table data into temp table and then add a update_stat column using below query and rest are same start from WHILE Loop.
SELECT * INTO ##tempTable
FROM Your_Table
ALTER TABLE dbo.dates ADD update_stat BIT DEFAULT(0)---rest are same..

not able to fetch date value using subqueries mysql

i have two tables called
1 table smartpos.pos_order_Id
+---------+--------------+---------+--------+--------------+----------------+------------+
| orderId | restaurantId | tableId | closed | customerName | customerNumber | dateorderd |
+---------+--------------+---------+--------+--------------+----------------+------------+
| 7 | 14 | 0 | yes | | | 21/03/2018 |
| 8 | 14 | 0 | yes | | | 21/03/2018 |
| 9 | 14 | 0 | no | | | 20/03/2018 |
| 10 | 14 | 0 | yes | soumya | 1234567890 | 21/03/2018 |
| 11 | 14 | 0 | yes | | | 21/03/2018 |
| 12 | 14 | 0 | yes | | | 21/03/2018 |
| 13 | 14 | 0 | yes | | | 21/03/2018 |
| 14 | 14 | 0 | yes | | | 20/03/2018 |
| 15 | 14 | 0 | no | | | 22/03/2018 |
+---------+--------------+---------+--------+--------------+----------------+------------+
2smartpos.pos_invoice
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| invoiceNumber | orderId | totalAmt | discountAmt | totalTaxAmt | grandTotal | paymentmode | paymentrefNum |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
| 1 | 7 | 200 | 34 | 46 | 212 | Cash | |
| 2 | 10 | 1200 | 200 | 280 | 1280 | Cash | |
| 3 | 1 | 720 | 34 | 120 | 806 | Cash | |
| 4 | 12 | 240 | 34 | 58 | 264 | Cash | |
| 5 | 13 | 330 | 32 | 83 | 381 | Cash | |
| 6 | 14 | 80 | 2 | 22 | 100 | Cash | |
+---------------+---------+----------+-------------+-------------+------------+-------------+---------------+
i want to fetch invoice details using restaurantId and two dates by providing restaurantId and dates as follows
select inv.invoiceNumber ,inv.totalAmt,inv.discountAmt,inv.totalTaxAmt,inv.grandTotal,i.dateorderd from smartpos.pos_invoice inv,smartpos.pos_order_Id i where inv.invoiceNumber in (select invv.invoiceNumber from smartpos.pos_invoice invv where invv.orderId in(select ii.orderId from smartpos.pos_order_Id ii where ii.closed='yes' and ii.restaurantId=14 and STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y'))) group by inv.invoiceNumber ;
out put:
+---------------+----------+-------------+-------------+------------+------------+
| invoiceNumber | totalAmt | discountAmt | totalTaxAmt | grandTotal | dateorderd |
+---------------+----------+-------------+-------------+------------+------------+
| 1 | 200 | 34 | 46 | 212 | NULL |
| 2 | 1200 | 200 | 280 | 1280 | NULL |
| 4 | 240 | 34 | 58 | 264 | NULL |
| 5 | 330 | 32 | 83 | 381 | NULL |
| 6 | 80 | 2 | 22 | 100 | NULL |
+---------------+----------+-------------+-------------+------------+------------+
but when i run above query it gives null values , how to fetch the date as well?
Its difficult to understand what you wrote but i think you query is much simple than it seems, try to use this approach :
select
inv.invoiceNumber ,
inv.totalAmt,
inv.discountAmt,
inv.totalTaxAmt,
inv.grandTotal,
i.dateorderd
from
smartpos.pos_invoice inv,
smartpos.pos_order_Id i
where
inv.orderId = i.orderId
and
i.closed='yes'
and
i.restaurantId=14
and
STR_TO_DATE(dateorderd,'%d/%m/%Y') between STR_TO_DATE('20/03/2018','%d/%m/%Y') and STR_TO_DATE('21/03/2018','%d/%m/%Y')
group by
inv.invoiceNumber;

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

Mysql rollup issue when grouping with quarter and date

I have this simple query:
select quarter(invitation_date) as `iQuarter`, invitation_date as `iDate`,count(*) as count from data group by iQuarter,iDate with rollup
I would expect to get a table like this:
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 133 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 20 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| NULL | NULL | 864 |
+----------+------------+-------+
But what I get has the total in the wrong position and before every quarters total we have a row with the next quarter with a count of one.
+----------+------------+-------+
| iQuarter | iDate | count |
+----------+------------+-------+
| 1 | 2010-02-23 | 1 |
| NULL | NULL | 1 |
| 1 | 2010-02-23 | 132 |
| 1 | 2010-03-02 | 14 |
| 1 | 2010-03-09 | 74 |
| 1 | 2010-03-15 | 102 |
| 1 | 2010-03-22 | 76 |
| 1 | 2010-03-30 | 16 |
| 2 | 2010-04-06 | 1 |
| NULL | NULL | 415 |
| 2 | 2010-04-06 | 19 |
| 2 | 2010-04-12 | 84 |
| 2 | 2010-04-19 | 85 |
| 2 | 2010-04-26 | 51 |
| 2 | 2010-05-03 | 73 |
| 2 | 2010-05-11 | 76 |
| 2 | 2010-05-18 | 54 |
| 2 | 2010-05-24 | 83 |
| 2 | 2010-06-01 | 73 |
| 2 | 2010-06-07 | 74 |
| 2 | 2010-06-24 | 81 |
| 2 | 2010-06-28 | 110 |
| 3 | 2010-07-06 | 1 |
| NULL | NULL | 864 |
+----------+------------+-------+
Invitation date is defined as:
+-------------------+---------------------------+------+-----+---------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------------+------+-----+---------------------+-------+
| invitation_date | date | NO | | | |
+-------------------+---------------------------+------+-----+---------------------+-------+
Anyone have a clue on why this is happening? Mysql version: 5.0.27

MYSQL Average datetime

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