免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2939 | 回复: 6
打印 上一主题 下一主题

一个sql,请教高手。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-05-23 13:38 |只看该作者 |倒序浏览
我写了一个sql,想列出所有公司的公司号和最后更新的时间:

select
  a.company_code,
  b.update_date
from
  updatehistory as a,
  (select max(update_date) from updatehistory where updatehistory.company_code=a.company_code) as b ;

可是,运行的时候,系统提示:
ERROR:  Relation 'a' does not exist.
问题好像出在:updatehistory.company_code=a.company_code,可是又不知道该怎么改。
请各位帮帮忙,看看我的sql,错在那里?
谢谢。

论坛徽章:
0
2 [报告]
发表于 2003-05-23 14:20 |只看该作者

一个sql,请教高手。

select
company_code,
max(update_date)
from
updatehistory  
group by  company_code ;

论坛徽章:
0
3 [报告]
发表于 2003-05-23 14:28 |只看该作者

一个sql,请教高手。

do you try?

论坛徽章:
0
4 [报告]
发表于 2003-05-23 15:49 |只看该作者

一个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
我很想知道原因。或者别的什么解决的办法。
各位有什么好的建议么?
谢谢。  

论坛徽章:
0
5 [报告]
发表于 2003-05-28 11:25 |只看该作者

一个sql,请教高手。

方法比较土,不过可以解决:
select company_code,update_date,input_money from test1
where trim(company_code||update_date) in(
select trim(company_code||max(update_date)) from test1 group by company_code)

当然,只用trim有破绽

论坛徽章:
0
6 [报告]
发表于 2003-05-28 18:55 |只看该作者

一个sql,请教高手。

To miya
谢谢你,这个sql我也想到过只是没有加上trim:
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 的确可以从test1中选出数据,不过如过数据量增大,那么运行速度会下降很多。
这个问题,我在CSDN上也问了,有兴趣的话可以看一下:
http://expert.csdn.net/Expert/topic/1839/1839714.xml

论坛徽章:
0
7 [报告]
发表于 2003-05-29 13:59 |只看该作者

一个sql,请教高手。

用in语句肯定在效率上有问题。
CSDN上的创建临时表的方法应该是效率最高的,只是需要建表,不太爽。而且在有些DB,如oracle,建表时隐含commit操作,除非在informix上用select 。。。into temp 。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP