免费注册 查看新帖 |

Chinaunix

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

请教高手一个sql脚本该怎么写?急! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-13 12:11 |只看该作者 |倒序浏览
有如下二张表:table1和table2,结构如下:
table1:id          totalamt
            1001            300.00
            1002            500.00
           ........            .........   
table2: id         date                   amt
           1001     20060101         150.00
           1001     20060410         120.00
           1001     20070328           30.00
           1002     20061001         280.00      
           1002     20061105         230.00
           ........
要求:根据table1中id的totalamt与table2对应id的amt合计相比对,不一致的输出table1.id
        如例中,table1中id为1001的totalamt为300,与table2中id号1001的amt合计相等(150+120+30),则不输出id号1001,
     id号1002的合计金额不等,则输出id号1002。
求教各位高手,谢谢!

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-20 06:20:00
2 [报告]
发表于 2007-07-13 13:14 |只看该作者
create view view_table2 as select id,sumamt=sum(amt) from table2 group by id
select a.id from table1 a,view_tables b where a.id=b.id and a.totalamt<>b.sumant
drop view view_table2

ase 12.5.1以后支持派生表 可以直接
select a.id from table1 a,(select id,sumamt=sum(amt) from table2 group by id) b where a.id=b.id and a.totalamt<>b.sumant

[ 本帖最后由 echoaix 于 2007-7-13 13:27 编辑 ]

论坛徽章:
0
3 [报告]
发表于 2007-07-13 13:26 |只看该作者
原帖由 zjnet6 于 2007-7-13 12:11 发表
有如下二张表:table1和table2,结构如下:
table1:id          totalamt
            1001            300.00
            1002            500.00
           ........            .........   
table2 ...

如果支持起源表(12.5.1以上,好像)
可以考虑用下面的方法
select t1.id from table1 t1,(select t2.id,sum(amt) totalamt from table2 t2 group by t2.id) t2 where t1.id = t2.id and t1.totalamt != t2.totalamt
不支持的话用下面的语句也可以
select t1.id from table1 t1,table2 t2 where t1.id = t2.id group by t1.id having t1.totalamt =sum(t2.amt)

论坛徽章:
0
4 [报告]
发表于 2007-07-13 13:29 |只看该作者
select t1.id from table1 t1,table2 t2 where t1.id = t2.id group by t1.id having t1.totalamt =sum(t2.amt) 这个错了
把相同的选出来了
换成
select t1.id from table1 t1,table2 t2 where t1.id = t2.id group by t1.id having t1.totalamt !=sum(t2.amt)
才是!

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-20 06:20:00
5 [报告]
发表于 2007-07-13 13:34 |只看该作者
select t1.id from table1 t1,table2 t2 where t1.id = t2.id group by t1.id having t1.totalamt =sum(t2.amt)这个好,一步到位,根据要求t1.totalamt =sum(t2.amt应该为<>

论坛徽章:
1
操作系统版块每日发帖之星
日期:2016-06-20 06:20:00
6 [报告]
发表于 2007-07-13 13:35 |只看该作者
有趣,同时发贴很有意思

论坛徽章:
0
7 [报告]
发表于 2007-07-13 13:51 |只看该作者
呵呵,有意思!
最近好像改版后cookies有点古怪,退出还会显示在线!

论坛徽章:
0
8 [报告]
发表于 2007-07-13 14:56 |只看该作者
谢谢朋友们的帮助,问题解决!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP