sql - count of two colums - mysql

i put in a database many line and i have this :
+----+---------------------+-------------+----------------+-----------+
| ID | time | os_name | program | last_line |
+----+---------------------+-------------+----------------+-----------+
| 14 | 2019-01-04 14:00:00 | titi | roro | 109 |
| 15 | 2019-01-04 14:00:00 | titi | roro | 109 |
| 16 | 2019-01-04 14:00:00 | gege | fefe | 109 |
i want :
+---------------------+-------------------------+-------------+-----------+-----------+
| time | nb_os_name_program | os_name | program | last_line |
+---------------------+-------------------------+-------------+-----------+-----------+
| 2019-01-04 14:00:00 | 2 | titi | roro | 109 |
| 2019-01-04 14:00:00 | 1 | gege | fefe | 109 |

Simple GROUP BY with aggregation would help you :
SELECT time, os_name, count(*) as nb_os_name, program, lastline,
FROM table t
GROUP BY time, os_name, program, lastline

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.

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 joining three tables and split into columns

I have three tables, mess_stock, mess_voucher, add_grocery.
Mess_stock table is below,
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| sno | voucher_id | particular_name | opening_balance | inward | outward | balance | pay_amount | pay_type |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
| 49 | 5 | 4 | 100 | 10 | 100 | 10 | 10.00 | 1 |
| 50 | 17 | 5 | 111 | 10 | 20 | 101 | 60.00 | 1 |
| 51 | 7 | 3 | 123 | 2 | 1 | 124 | 300.00 | 1 |
| 52 | 7 | 1 | 123 | 20 | 20 | 123 | 500.00 | 2 |
| 53 | 14 | 8 | 100 | 5 | 95 | 10 | 60.00 | 2 |
+-----+------------+-----------------+-----------------+--------+---------+---------+------------+----------+
Mess_voucher table is below
+------------+--------------+--------------+
| voucher_id | voucher_name | voucher_date |
+------------+--------------+--------------+
| 5 | VG1001 | 2015-02-19 |
| 6 | VG1001 | 2015-02-20 |
| 7 | VG1002 | 2015-02-20 |
| 8 | VG1002 | 2015-02-19 |
| 9 | MS1001 | 2015-02-20 |
| 10 | VG10012 | 2015-02-19 |
| 11 | 0 | 2015-02-23 |
| 12 | 1 | 2015-02-24 |
| 13 | MS1001 | 2015-02-25 |
| 14 | MS1001 | 2015-02-28 |
| 15 | VG1003 | 2015-02-28 |
| 16 | MS1001 | 2015-02-19 |
| 17 | MS1001 | 2015-02-21 |
+------------+--------------+--------------+
Add_grocery table is below
+-----+-----------------+------------------+
| sno | particular_name | particular_price |
+-----+-----------------+------------------+
| 1 | Rice | 25.00 |
| 3 | Mango | 150.00 |
| 4 | Coconut | 22.00 |
| 5 | Banana | 6.00 |
| 6 | Raddish | 12.00 |
| 7 | Apple | 150.00 |
| 8 | Pumkin | 12.00 |
+-----+-----------------+------------------+
I want to group the sum of pay_amount of mess_stock table. I have used the below query
SELECT opening_balance AS ope_stock,
balance AS clo_stock,
SUM(IF(pay_type = 1, pay_amount, 0)) mess_pay,
SUM(IF(pay_type=2, pay_amount, 0)) est_pay
FROM mess_stock;
That works fine. The particular_name is the auto increment id of add_grocery table. I need the inward outward amount total. For example the inward amount 10 means it has to get the particular_price from add_grocery using the particular_name provided in the mess_stock table, similarly I need all the answer. And I want to sort that by date wise. The date of the entry is stored in the mess_voucher table that is connected to mess_stock table.
Try this it will work :
Use Inner Join :
SELECT t2.`particular_name`,t1.`inward`,t1.`outward`,t2.`particular_price`,t3.`voucher_date` from Mess_stock t1 JOIN Add_grocery t2 ON t1.`particular_name`=t2.`sno` JOIN Mess_voucher t3 ON t3.`voucher_id`=t1.`voucher_id` ORDER BY t3.`voucher_date` DESC

Performing an operation within EACH GROUP of rows

I have a table like below.
+------------+------------------+-------------------------------+-------------+
| day | workflow_step_id | UNIX_TIMESTAMP(finished_time) | workflow_id |
+------------+------------------+-------------------------------+-------------+
| 2014-04-30 | 1 | 1398852780 | 1 |
| 2014-04-30 | 17 | 1398871213 | 2 |
| 2014-04-30 | 6 | 1398872807 | 1 |
| 2014-04-30 | 22 | 1398898676 | 2 |
| 2014-04-30 | 11 | 1398898234 | 1 |
| 2014-04-30 | 16 | 1398866813 | 2 |
| 2014-04-30 | 5 | 1398869940 | 1 |
| 2014-04-30 | 21 | 1398893419 | 2 |
| 2014-04-30 | 10 | 1398894136 | 1 |
| 2014-04-30 | 15 | 1398861385 | 2 |
| 2014-04-30 | 4 | 1398860271 | 1 |
| 2014-04-30 | 20 | 1398888252 | 2 |
| 2014-04-30 | 9 | 1398886916 | 1 |
| 2014-04-30 | 14 | 1398863922 | 2 |
| 2014-04-30 | 3 | 1398865682 | 1 |
| 2014-04-30 | 19 | 1398881994 | 2 |
| 2014-04-30 | 8 | 1398882497 | 1 |
| 2014-04-30 | 13 | 1398852785 | 2 |
| 2014-04-30 | 2 | 1398856674 | 1 |
| 2014-04-30 | 18 | 1398878836 | 2 |
| 2014-04-30 | 7 | 1398878949 | 1 |
| 2014-04-30 | 12 | 1398850920 | 2 |
| 2014-05-01 | 12 | 1398932040 | 2 |
| 2014-05-01 | 1 | 1398938880 | 1 |
| 2014-05-01 | 17 | 1398957830 | 2 |
| 2014-05-01 | 6 | 1398961385 | 1 |
| 2014-05-01 | 22 | 1398996388 | 2 |
| 2014-05-01 | 11 | 1398994543 | 1 |
| 2014-05-01 | 16 | 1398946714 | 2 |
| 2014-05-01 | 5 | 1398959182 | 1 |
| 2014-05-01 | 21 | 1398989773 | 2 |
| 2014-05-01 | 10 | 1398979568 | 1 |
| 2014-05-01 | 15 | 1398949939 | 2 |
| 2014-05-01 | 4 | 1398953732 | 1 |
| 2014-05-01 | 20 | 1398980246 | 2 |
| 2014-05-01 | 9 | 1398971281 | 1 |
| 2014-05-01 | 14 | 1398940775 | 2 |
| 2014-05-01 | 3 | 1398944207 | 1 |
| 2014-05-01 | 19 | 1398970295 | 2 |
| 2014-05-01 | 8 | 1398967778 | 1 |
| 2014-05-01 | 13 | 1398940935 | 2 |
| 2014-05-01 | 2 | 1398947433 | 1 |
| 2014-05-01 | 18 | 1398966959 | 2 |
| 2014-05-01 | 7 | 1398965931 | 1 |
-------------------------------------------------------------------------------
command:
SELECT day,
stats.workflow_step_id,
Max(Unix_timestamp(finished_time)) - Min(Unix_timestamp(finished_time)),
workflow_id
FROM modeling_dashboard_workflow_stats stats
INNER JOIN modeling_dashboard_workflow_step step
ON stats.workflow_step_id = step.workflow_step_id
ORDER BY day;
In this example only has two days and two workflow_ids, but it can have any number of days or workflow_ids.
I want to calculate the difference between maximum timestamp and minimum timestamp for each workflow_id for each day.
It should look something like this.
+------------+-------------------------------------------------------------+
| day | MAX(timestamp) - MIN(timestamp) |
+------------+-------------------------------------------------------------+
| 2014-04-30 | difference bw max&min timestamp with workflow_id=1 in 04-30 |
| 2014-04-30 | difference bw max&min timestamp with workflow_id=2 in 04-30 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=1 in 05-01 |
| 2014-05-01 | difference bw max&min timestamp with workflow_id=2 in 05-01 |
----------------------------------------------------------------------------
How can I do this?
Ideally it puts results for workflow_id = 1 and those for workflow_id = 2 in different columns, (day, MAX-MIN for id=1, MAX-MIN for id=2), but that's just my next step.
SELECT day, workflow_id
, MAX(UNIX_TIMESTAMP(finished_time)) - MAX(UNIX_TIMESTAMP(finished_time))
FROM modeling_dashboard_workflow_stats
GROUP BY day, workflow_id
try this:
select day
,workflow_step_id
,MAX(UNIX_TIMESTAMP(finished_time)) - MIN(UNIX_TIMESTAMP(finished_time))
from modeling_dashboard_workflow_stats
group by day
,workflow_step_id

comparing the same table over two rows in MySQL

So this is what i have
Table : Bill
+------+------------+-----------------+---------------------+
| id | patient_id | bill_number | confirmed_date |
+------+------------+-----------------+---------------------+
| 14 | 32 | 4657 | 2012-07-06 04:11:05 |
| 15 | 33 | 4567 | 2012-07-07 05:11:05 |
| 16 | 34 | 4568 | 2012-07-08 06:11:05 |
| 17 | 35 | 7445 | 2012-08-08 07:11:05 |
+------+------------+-----------------+---------------------+
Table: Claim
+------+---------+------------+-------+--------------+---------------------+
| id | bill_id | patient_id | level | claim_format | confirmed_date |
+------+---------+------------+-------+--------------+---------------------+
| 10 | 14 | 32 | 1 | 1500 | 2012-08-10 10:57:17 |
| 11 | 14 | 32 | 1 | UB04 | 2012-08-10 11:01:42 |
| 12 | 15 | 33 | 1 | 1500 | 2012-09-10 13:57:17 |
| 13 | 15 | 33 | 1 | UB04 | 2012-09-10 12:01:42 |
| 14 | 16 | 34 | 1 | 1500 | 2012-10-10 12:57:17 |
| 15 | 16 | 34 | 1 | UB04 | 2012-10-10 13:01:42 |
| 16 | 17 | 35 | 1 | 1500 | 0012-11-10 15:57:17 |
| 17 | 17 | 35 | 1 | UB04 | 2012-11-10 14:01:42 |
+------+---------+------------+-------+--------------+---------------------+
I want to update the confirmed_date column of bill table with the confirmed_date of claim table after comparing the greater of the two dates for each bill_id(bill_id and patient_id in claims are foreign keys to id and patient_id in bill)
Did i make myself clear enough?
UPDATE Bill b
SET b.confirmed_date = ( SELECT MAX(confirmed_date) FROM Claim c WHERE b.id = c.bill_id)