求一条sql的简化

晓疯馋曰 2009-08-27 11:32:00
select id,
sl=(select SUM(sl) from table1 b where id in (select ID from dbo.GetChild(a.id))),
je=(select SUM(je) from table1 b where id in (select ID from dbo.GetChild(a.id)))
from table1 a

其中 dbo.GetChild 取出下属ID.
...全文
122 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 cqslzhao 的回复:]
谢谢你的热心!
[/Quote]
不客气哦
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
谢谢你的热心!
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 cqslzhao 的回复:]
引用 15 楼 feixianxxx 的回复:
SQL codeselect a.id,
sl=SUM(b.sl) ,
je=SUM(b.je)
from (select ID from dbo.GetChild(id)) a join table1 b on b.id=a.id group by a.id
这样试试看


dbo.GetChild(id)) 这个ID应该是 b.ID吧?

这次真的出现  无法绑定多个部分 了.

[/Quote]
呵呵 好像是不对。。。
这样反而复杂 算了。。。不想了 楼主问题解决就好 呵呵 半夜脑袋晕
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 feixianxxx 的回复:]
SQL codeselect a.id,
sl=SUM(b.sl) ,
je=SUM(b.je)
from (select ID from dbo.GetChild(id)) a join table1 b on b.id=a.id group by a.id
这样试试看
[/Quote]

dbo.GetChild(id)) 这个ID应该是 b.ID吧?

这次真的出现 无法绑定多个部分 了.
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 cqslzhao 的回复:]
不好意思,是可以的,非常感谢!

结贴!
[/Quote]
哥们别急 再试试15楼的
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
select a.id, 
sl= SUM(b.sl) ,
je= SUM(b.je)
from (select ID from dbo.GetChild(id)) a join table1 b
on b.id =a.id
group by a.id

这样试试看
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
不好意思,是可以的,非常感谢!

结贴!
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复

select a.id,
sl= SUM(b.sl) ,
je= SUM(b.je)
from table1 a join table1 b
on b.id in (select ID from dbo.GetChild(a.id))group By a.id
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 cqslzhao 的回复:]
GetChild(a.id))

不行, a.id 报错,无法绑定多个部分.
[/Quote]
哪个写法出错了
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
理论上是都可以转换为join方式的吧?
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
GetChild(a.id))

不行, a.id 报错,无法绑定多个部分.
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 feixianxxx 的回复:]
引用 6 楼 cqslzhao 的回复:
引用 3 楼 feixianxxx 的回复:
SQL codeselect a.id,
sl=SUM(sl) ,
je=SUM(je)from table1 a , table1 bwhere b.idin (select IDfrom dbo.GetChild(a.id))groupby a.id


谢谢!比我写的好.

这个还能不能转为 join 的方式呢?



SQL codeselect a.id,
sl=SUM(b.sl) ,
je=SUM(b.je)from table1 ajoin table1 bon b.idin (select IDfrom dbo.GetChild(a.id))
你可以去试试
[/Quote]

select a.id, 
sl= SUM(b.sl) ,
je= SUM(b.je)
from table1 a join table1 b
on b.id in (select ID from dbo.GetChild(a.id))
group By a.id
feixianxxx 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cqslzhao 的回复:]
引用 3 楼 feixianxxx 的回复:
SQL codeselect a.id,
sl=SUM(sl) ,
je=SUM(je)from table1 a , table1 bwhere b.idin (select IDfrom dbo.GetChild(a.id))groupby a.id


谢谢!比我写的好.

这个还能不能转为 join 的方式呢?


[/Quote]

select a.id,
sl= SUM(b.sl) ,
je= SUM(b.je)
from table1 a join table1 b
on b.id in (select ID from dbo.GetChild(a.id))

你可以去试试
--小F-- 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 cqslzhao 的回复:]
引用 3 楼 feixianxxx 的回复:
SQL codeselect a.id,
sl=SUM(sl) ,
je=SUM(je)from table1 a , table1 bwhere b.idin (select IDfrom dbo.GetChild(a.id))groupby a.id


谢谢!比我写的好.

这个还能不能转为 join 的方式呢?


[/Quote]

没有连接字段了 不好转了
晓疯馋曰 2009-08-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 feixianxxx 的回复:]
SQL codeselect a.id,
sl=SUM(sl) ,
je=SUM(je)from table1 a , table1 bwhere b.idin (select IDfrom dbo.GetChild(a.id))groupby a.id
[/Quote]

谢谢!比我写的好.

这个还能不能转为 join 的方式呢?

jinjazz 2009-08-27
  • 打赏
  • 举报
回复
这个是bom表的计算
feixianxxx 2009-08-27
  • 打赏
  • 举报
回复
select a.id, 
sl= SUM(b.sl) ,
je= SUM(b.je)
from table1 a , table1 b
where b.id in (select ID from dbo.GetChild(a.id))
group by a.id

xiugai
feixianxxx 2009-08-27
  • 打赏
  • 举报
回复
select a.id, 
sl= SUM(sl) ,
je= SUM(je)
from table1 a , table1 b
where b.id in (select ID from dbo.GetChild(a.id))
group by a.id
晓疯馋曰 2009-08-27
  • 打赏
  • 举报
回复
sl=(select SUM(sl) from table1 b where id in (select ID from dbo.GetChild(a.id))),
je=(select SUM(je) from table1 b where id in (select ID from dbo.GetChild(a.id)))
显得重复.
华夏小卒 2009-08-27
  • 打赏
  • 举报
回复
够精简了

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧