- 论坛徽章:
- 0
|
一个sql,请教高手。
TO:zhtzn
谢谢你。
不好意思,我写的东西有些问题,实际上我想解决的问题是:
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 company_code,max(update_date),input_money from test1 group by company_code,input_money;
选出来得结果是:
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
所以,我想用
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
这个sql,试一试,不过不好用。
出现提示:ERROR: Relation 'a' does not exist
我很想知道原因。或者别的什么解决的办法。
各位有什么好的建议么?
谢谢。 |
|