junjoi:
¿Cómo obtener un valor diferente en la misma tabla, los datos se cambia basa en las horas
Estructura de la tabla
Current Table
country | hour | date | total
China | 22 | 18-March-2020 | 25777
China | 23 | 18-March-2020 | 35477
China | 24 | 18-March-2020 | 45777
India| 22 | 18-March-2020 | 4547
India | 23 | 18-March-2020 | 5477
India | 24 | 18-March-2020 | 6478
el resultado fuera está buscando
China | 23 | 35477 - 25777
China | 24 | 45777-35477
Igual que para otros países.
GMB:
En MySQL 8.0, puede utilizar las funciones de ventana:
select
t.*,
concat(total, ' - ', lag(total) over(partition by country, date order by hour)) info
from mytable
Si desea eliminar la primera fila por cada grupo, entonces se puede utilizar una subconsulta:
select *
from (
select
t.*,
concat(total, ' - ', lag(total) over(partition by country, date order by hour)) info
from mytable
) t
where info is not null
En versiones anteriores, se puede auto-unirse a la tabla:
select t.*, concat(t.total, ' - ', tlag.total) info
from mytable t
inner join mytable tlag
on tlag.country = t.country and tlag.date = t.date and tlag.hour = t.hour - 1