免费注册 查看新帖 |

Chinaunix

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

sql语句问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-06-02 16:35 |只看该作者 |倒序浏览
table 1 :
(a char(,
fistdate smalldatetime NULL,
lastdate smalldatetime NULL)

table 2:
(checkdate smalldatetime,
a char()

table 1 data:
a          firstdate         lastdate
------------------------------------------
1234    NULL              NULL
5678    NULL              NULL


table 2 data:
checkdate                  a
-------------------------------------------
2006-04-01               1234
2006-04-02               1234
2006-04-05               1234
2006-04-01               5678
2006-05-01               5678

现在要将table1的字段维护成为对应table2的min和max值
该如何写?

尝试如下:
update table1
set table1.firstdate = min(table2.checkdate),
      table1.lastdate = max(table2.checkdate)
from table1,table2
where table1.a = table2.a

发现无法聚合,update错误,group by 又不能在update中使用

如何在update关联2张以上表的时候对应使用聚合????

大家帮忙看看,我一直没能想出来,现在暂时用临时表将table2先做了一次group by,再用临时表关联table1进行update,感觉好像不对,来这里问问看大家有没有在一个语句中的解决办法

论坛徽章:
0
2 [报告]
发表于 2006-06-12 14:10 |只看该作者
没有方法?

论坛徽章:
0
3 [报告]
发表于 2006-06-13 01:25 |只看该作者
if it's Oracle, you can do this way:

update t1 set t1.firstdate=t3.firstdate
from t1,(select distinct a,min(t2.checkdate) firstdate from t2 group by a) t3
where t1.a = t3.a

Sybase needs to work on this.

You can't use a derived table in the FROM clause of an UPDATE or DELETE statement. You have to populate a temp table first.
You can't even use a derived table in the FROM clause of a SELECT statement in lower version.

论坛徽章:
0
4 [报告]
发表于 2006-06-13 15:34 |只看该作者
收到并学习了,照这么说, SYBASE还是得创建临时表
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP