语法分组依据
syntax group by
我需要计算性能,即 7 月份的最大里程减去 8 月份的最大里程,试试这个有效但只打印相同数量的查询。
这是我的查询:
select vehicle_id, max(odometro) as cierre,sum(cantidad) as lts,((select max(odometro)
from vehicle_consumptions where fecha between '2020-07-01' and '2020-07-31' group by vehicle_id) - (select max(odometro) from vehicle_consumptions where fecha between '2020-08-01' and '2020-08-30' group by vehicle_id))
as recorrido,
((select max(odometro)
from vehicle_consumptions where fecha between '2020-07-01' and '2020-07-31') -
(select max(odometro) from vehicle_consumptions where fecha between '2020-08-01' and '2020-08-30'))/sum(cantidad) as rendimiento
from vehicle_consumptions where EXTRACT(month from fecha)= 08 group by vehicle_id
我想你想要条件聚合:
select vehicle_id,
max(case when fecha < '2020-08-01' then odometro end)
- max(case when fecha >= '2020-08-01' then odometro end)
as odometro_diff
from vehicle_consumptions
where fecha between '2020-07-01' and '2020-08-31'
group by vehicle_id
在 Postgres 中,我们可以使用标准的 filter
子句和日期文字(假设 fecha
是一种类似日期的数据类型——它应该是):
select vehicle_id,
max(odometro) filter(fecha < date '2020-08-01')
- max(odometro) filter(where fecha >= date '2020-08-01')
as odometro_diff
from vehicle_consumptions
where fecha between date '2020-07-01' and date '2020-08-31'
group by vehicle_id
最后:如果你的日期有时间成分,我会推荐半开间隔,所以 where
子句应该是:
where fecha >= date '2020-07-01' and fecha < '2020-09-01'
您可以使用条件聚合:
select vehicle_id,
(max(case when fecha between '2020-08-01' and '2020-08-31' then odometro end)
max(case when fecha between '2020-07-01' and '2020-07-31' then odometro end)
) as diff
from vehicle_consumption
group by vehicle_id;
在 Postgres 中,您可以将其表述为:
select vehicle_id,
(max(odometro) filter (where extract(month from fecha) = 7) -
max(odometro) filter (where extract(month) from fecha) = 8)
) as diff
from vehicle_consumption
where fecha >= '2020-07-01' and fecha < '2020-09-01'
group by vehicle_id;
where
子句将处理时间限制为仅两个月,这应该会使查询执行得更好。然后它只使用月份来过滤 max()
.
我需要计算性能,即 7 月份的最大里程减去 8 月份的最大里程,试试这个有效但只打印相同数量的查询。
这是我的查询:
select vehicle_id, max(odometro) as cierre,sum(cantidad) as lts,((select max(odometro)
from vehicle_consumptions where fecha between '2020-07-01' and '2020-07-31' group by vehicle_id) - (select max(odometro) from vehicle_consumptions where fecha between '2020-08-01' and '2020-08-30' group by vehicle_id))
as recorrido,
((select max(odometro)
from vehicle_consumptions where fecha between '2020-07-01' and '2020-07-31') -
(select max(odometro) from vehicle_consumptions where fecha between '2020-08-01' and '2020-08-30'))/sum(cantidad) as rendimiento
from vehicle_consumptions where EXTRACT(month from fecha)= 08 group by vehicle_id
我想你想要条件聚合:
select vehicle_id,
max(case when fecha < '2020-08-01' then odometro end)
- max(case when fecha >= '2020-08-01' then odometro end)
as odometro_diff
from vehicle_consumptions
where fecha between '2020-07-01' and '2020-08-31'
group by vehicle_id
在 Postgres 中,我们可以使用标准的 filter
子句和日期文字(假设 fecha
是一种类似日期的数据类型——它应该是):
select vehicle_id,
max(odometro) filter(fecha < date '2020-08-01')
- max(odometro) filter(where fecha >= date '2020-08-01')
as odometro_diff
from vehicle_consumptions
where fecha between date '2020-07-01' and date '2020-08-31'
group by vehicle_id
最后:如果你的日期有时间成分,我会推荐半开间隔,所以 where
子句应该是:
where fecha >= date '2020-07-01' and fecha < '2020-09-01'
您可以使用条件聚合:
select vehicle_id,
(max(case when fecha between '2020-08-01' and '2020-08-31' then odometro end)
max(case when fecha between '2020-07-01' and '2020-07-31' then odometro end)
) as diff
from vehicle_consumption
group by vehicle_id;
在 Postgres 中,您可以将其表述为:
select vehicle_id,
(max(odometro) filter (where extract(month from fecha) = 7) -
max(odometro) filter (where extract(month) from fecha) = 8)
) as diff
from vehicle_consumption
where fecha >= '2020-07-01' and fecha < '2020-09-01'
group by vehicle_id;
where
子句将处理时间限制为仅两个月,这应该会使查询执行得更好。然后它只使用月份来过滤 max()
.