为不同的列选择最近的结果-SQL [英] select recent results for distinct columns- SQL
本文介绍了为不同的列选择最近的结果-SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,请帮忙
我试图在最新日期获得每辆车的等级.
样本表:
Hi guys, please help
Im trying to get the grade for each car at the latest date.
Sample table:
car-index installed_date firebar-grade frame-grade
CAR5 05-Jul-12 B A
CAR3 31-Jan-12 A A
CAR1 05-Apr-12 C C
CAR1 24-May-12 D D
CAR3 18-May-12 C C
CAR1 08-Feb-12 C A
CAR1 18-May-12 C B
CAR5 24-May-12 D C
CAR3 11-May-12 C B
CAR5 22-Aug-12 B B
CAR5 17-Sep-12 A B
预期结果:
desired results:
car-index installed_date firebar-grade frame-grade
CAR1 24-May-12 D D
CAR3 18-May-12 C C
CAR5 17-Sep-12 A B
我已经尝试过了,但是结果不正确.
I''ve tried this, but the results are incorrect.
select car_index, frame_grade, firebar_grade, max(installed_date) as recentdate from car
group by car_index, frame_grade, firebar_grade;
推荐答案
select * from car c where c.installed_date = (select MAX(installed_date) from car cr where cr.[car-index] = c.[car-index])
试试这个
Try this
SELECT C.car_index,MAX(C.firebar_grade),MAX(C.frame_grade) FROM Car C
Group by C.car_index
希望这会有所帮助,如果是,则接受并投票回答,否则返回您的查询
--Rahul D.
Hope this helps if yes then accept and vote the answer otherwise revert back with your queries
--Rahul D.
这篇关于为不同的列选择最近的结果-SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文