Domanda

Poiché il vocabolario che descrive ciò che mi piacerebbe fare può essere così disparato, non ho avuto fortuna a capire perché MySQL 5.6 stia facendo quello che fa.

Considera le seguenti due semplici domande:

#1
select sum(amount) as amt,
    (sum(amount) * (0.000+coalesce((select gas from gas_prices gp where gp.yw=yearweek(gl.stamp,0)),3.50))) as cost
from gas_log gl
where date_format(gl.stamp,'%X')=2013;

#2
select sum(amount) as amt,
    (sum(amount) * (0.000+coalesce((select gas from gas_prices gp where gp.yw=yearweek(gl.stamp,0)),3.50))) as cost
from gas_log gl
where date_format(gl.stamp,'%X')=2013 group by yearweek(gl.stamp,0);

La seconda query è identica alla prima, con un semplice gruppo_by per ottenere i totali settimanali anziché i totali annuali. Entrambe le domande hanno output di spiegazioni quasi identiche:

+----+--------------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
| id | select_type        | table | type | possible_keys | key  | key_len | ref  | rows | Extra                                        |
+----+--------------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+
|  1 | PRIMARY            | gl    | ALL  | NULL          | NULL | NULL    | NULL | 7428 | Using where; Using temporary; Using filesort |
|  2 | DEPENDENT SUBQUERY | gp    | ALL  | yw            | NULL | NULL    | NULL |   52 | Using where                                  |
+----+--------------------+-------+------+---------------+------+---------+------+------+----------------------------------------------+


+----+--------------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type        | table | type | possible_keys | key  | key_len | ref  | rows | Extra       |
+----+--------------------+-------+------+---------------+------+---------+------+------+-------------+
|  1 | PRIMARY            | gl    | ALL  | NULL          | NULL | NULL    | NULL | 7428 | Using where |
|  2 | DEPENDENT SUBQUERY | gp    | ALL  | yw            | NULL | NULL    | NULL |   52 | Using where |
+----+--------------------+-------+------+---------------+------+---------+------+------+-------------+

gas_log Contiene un timestamp di quando il gas è stato pompato e quanto è stato pompato. gas_prices Somiglia a questo:

10:11:09> select * from gas_prices limit 2;
+------------+--------+-------+--------+
| date       | yw     | gas   | diesel |
+------------+--------+-------+--------+
| 2013-01-07 | 201301 | 3.235 |  3.870 |
| 2013-01-14 | 201302 | 3.265 |  3.834 |
+------------+--------+-------+--------+

Per la prima query, MySQL esegue la sottoquery solo una volta e usa quel valore (trovato corrispondente alla prima riga recuperata da gas_log Contro la sua settimana corrispondente in gas_prices) Per moltiplicare la somma di tutti i galloni effettuati nel registro del gas, mentre nella seconda query fa quello che sto effettivamente dopo: eseguire la sottoquery per ciascuna delle 52 settimane raggruppate nel 2013, abbinando il prezzo del gas di ogni settimana di conseguenza.

Usando with rollup sul group by fornisce un risultato interessante; Invece di usare il primo prezzo del gas nel timerange, usa l'ultimo! Pertanto, fornisce ancora un totale di costo errato per qualsiasi tempo che si estende per più di una settimana.

C'è un modo per riscrivere la query per far presentare un totale di MySQL per un determinato timerge, ma ancora ciascuno gas_log riga al prezzo corrispondente in gas_prices?

Nessuna soluzione corretta

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange
scroll top