如果用一条sql语句,把一个表内,具有相同父类编号的子类别号的数量累加到父类编号上

booksfount 2004-09-11 04:14:48
表名叫 bs_rep_day_lh ,

现在的数据是这样的:
-----------------------------
c_ptid F_STOCKSYS
----------------------------
001 null
001001 10
001002 20

处理后是这样的
-----------------------------
c_ptid F_STOCKSYS
----------------------------
001 30 --这里变为30
001001 10
001002 20


我的sql是这样写的:但是报子查询返回的结果是对于一条记录的,我不知道那里错了,谢谢帮助
select a.c_ptid,
(
select sum(isnull(a.F_STOCKSYS,0))
from bs_rep_day_lh b
where b.D_date = '2004-8-1' and left(a.c_ptid,len(b.c_ptid)) = b.c_ptid
) as depots
from bs_rep_day_lh a ,bs_rep_day_lh b
where a.d_date = '2004-8-1' and a.d_date = b.d_date
and left(a.c_ptid,len(b.c_ptid)) = b.c_ptid
group by a.c_ptid
order by a.c_ptid

...全文
162 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
booksfount 2004-09-11
  • 打赏
  • 举报
回复
谢谢
booksfount 2004-09-11
  • 打赏
  • 举报
回复
谢谢,我试验一下,待会儿要回去了,星期一来结帖子,谢谢!
zjcxc 元老 2004-09-11
  • 打赏
  • 举报
回复
--用join实现查询
select a.c_ptid,F_STOCKSYS=sum(b.F_STOCKSYS)
from bs_rep_day_lh a
left join bs_rep_day_lh b
on a.D_date = '2004-8-1'
and b.D_date = '2004-8-1'
and b.c_ptid like a.c_ptid+'%'
group by a.c_ptid
order by a.c_ptid
zjcxc 元老 2004-09-11
  • 打赏
  • 举报
回复
--直接更新
update a set F_STOCKSYS=isnull((
select sum(F_STOCKSYS) from bs_rep_day_lh
where D_date = '2004-8-1'
and c_ptid like a.c_ptid+'%'),0)
from bs_rep_day_lh a
where d_date = '2004-8-1'
zjcxc 元老 2004-09-11
  • 打赏
  • 举报
回复
--查询
select c_ptid,F_STOCKSYS=isnull((
select sum(F_STOCKSYS) from bs_rep_day_lh
where D_date = '2004-8-1'
and c_ptid like a.c_ptid+'%'),0)
from bs_rep_day_lh a
where d_date = '2004-8-1'

34,571

社区成员

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

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