select a.company_code,a.input_money, b.update_date from test1 a ,(select company_code,max(update_date) as update_date from test1 group by company_code) b where b.company_code = a.company_code and a.update_date=b.update_date
select company_code,max(update_date),input_money from td_temp group by company_code; 我试过了,行!
上面写的不对!重写: 先建立一临时表: create table td_temp1 select company_code,max(update_date) update_date from td_temp group by company_code; 再联立两个表: select a.* from td_temp a,td_temp1 b where a.company_code=b.company_code and a.update_date=b.update_date 肯定行!
运行结果:
company_code | max | input_money
--------------+------------+-------------
01 | 2003/03 | 10000
01 | 2003/07 | 20000
01 | 2003/05 | 30000
01 | 2003/06 | 40000
02 | 2003/04 | 20000
02 | 2003/05 | 30000
(6 rows)
结果不对。
只懂MySQL,看不懂:P
谢谢lierq(李子) ,Arbow(◎_◎) ,swotcoder(苦 丁) ,neumqp(风之子) 对这个帖子的关注和参与。
如果各位还有更好的方法,或者别的什么方法,希望大家贴出来。
谢谢各位。
from td_temp
group by company_code;
我试过了,行!
先建立一临时表:
create table td_temp1
select company_code,max(update_date) update_date
from td_temp
group by company_code;
再联立两个表:
select a.* from td_temp a,td_temp1 b
where a.company_code=b.company_code
and a.update_date=b.update_date
肯定行!
如果当前表(td_temp)与自己(td_temp)联立,数据量大时,sql语句的效率很低,数据库容易死掉。