with t as (select
any_value(gps_speed) as "speed",
(with t as是针对一个别名,针对多个时就会用到多个as;any_value在MySQL中就是与group_by捆绑使用的,作用是分组;)
max(gps_date) (Max()一般用来找出最大数值或日期值,但MySQL允许它返回任意列中的最大值,包括文本列中的最大值。在用于文本数据时,若数据按相应列排序,则max()返回最后一行。Max()忽略空值行)
from t_bus_status group by bus_plate)
select
sum(case when t.speed<=40 then 1 else 0 end) as "车速低于40码",
sum(case when t.speed>40 then 1 else 0 end) as "车速超过40码"
from t;(两条中的sum是求和函数,加起来的意思,case whent.speed<=40 then 1 else 0 end主要意思是判断取得字段是否等于某个值,如果等于就等于一个值,反之则等于另一个值。第一个就是速度小于或者等于40就是1且输出车速低于40码,否则就是0结束,第二条就是速度大于40就是1输出车速大于40码,反之值为0结束)