一个select的问题,请教各位。
pwair 2003-05-27 01:10:49 问题:
talbe:
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/03 | 10000
01 | 2003/04 | 20000
01 | 2003/05 | 30000
01 | 2003/06 | 40000
01 | 2003/07 | 20000
02 | 2003/02 | 20000
02 | 2003/03 | 20000
02 | 2003/04 | 20000
02 | 2003/05 | 30000
想把01和02中最新的update_date所对应的input_money选出来。
也就是:
company_code | update_date | input_money
--------------+-------------+-------------
01 | 2003/07 | 20000
02 | 2003/05 | 30000
试过:
select a.company_code,a.input_money, b.update_date from test1 a,(select max(update_date) from test1 where test1.company_code = a.company_code) b
系统提示:
ERROR: Relation 'a' does not exist
还试过:
select t1.company_code, t1.update_date, t1.input_money from test1 as t1
where (company_code,update_date) in (select t2.company_code, max(t2.update_date) from test1 as t2 group by t2.company_code);
这个sql可以选出数据,可是如果数据量增大,运行速度会变慢很多。
请教各位有没有什么好的办法可以提高运行效率。
(以上的sql查询和结果都是在Postgresql下运行的)