test表 有违法类型 车牌号 违法时间等字段,对同一辆车的压线、超速只查询最早的那条,其它违法类型均查询?
select 违法类型,车牌号,min(时间) from test
按时间排序,按违反类型分组
select * from (select count(*) n,违法类型,车牌号 from test group by 车牌号,违法类型 order by 时间) where n =1
select 类型,车牌,min(时间) from test group by 车牌 having 违法类型='压线' union all
select 类型,车牌,min(时间) from test group by 车牌 having 违法类型='超速' union all
select 类型,车牌,时间 from test where 违法类型 not in('压线','超速')
自己的sql不好,可以参考下,我也不知道对不对 哈哈
select *
from (select a.*,
row_number() over(partition by 车牌号, 违法类型,(case
when 违法类型 in
('压线', '超速') then
1
else
rowid
end) order by 违法时间 asc) rn
from test表 a)
where rn = 1;
这样应该能实现你的需求