我需要一些帮助: 假设我有以下记录:
Driver Date Consultant
1 1/01/2010 A
1 3/04/2010 A
1 3/04/2010 A
1 25/06/2012 B
2 24/03/2012 C
3 13/01/2012 A
3 15/01/2012 B
3 15/01/1012 C
日期和顾问来自QUOTES表,驱动程序来自DRIVER表。与驱动程序ID相关。
我需要将结果集看起来如下:
Driver Quote Count First date Last Date Consultant
1 4 1/01/2010 25/06/2012 B
2 1 24/03/2012 24/03/2012 C
3 3 13/01/2012 15/01/1012 C
在MySQL中,如何让结果集中的顾问成为最后一位参与QUOTE的顾问?
答案 0 :(得分:1)
可以使用显式连接执行此操作:
select q.*, t.consultant
from (select driver, count(*) as quotecount, min(date) as FirstDate, max(date) as lastdate
from t
group by driver
) q join
t
on t.driver = q.driver and t.date = q.lastdate