求一个存储过程。在线等。好用就结贴。

bwguang 2006-10-13 06:42:24
一个表主键是JAN,另外还有SEC,BRA,Status,Num几个键,每个BRA对应多个SEC,每个SEC又对应多个JAN。每个JAN对应一个Status,Status有0,1,3,9几个状态。要写一个存储过程,得出表里数据的状态。、结果集包括SEC,BRA,Num,状态字Sta 四列。状态字的定义是,如果每个BRA和SEC中的商品JAN的Status 全部为0 状态字Sta=0 ,全部为1 Sta=4,全部为 9 Sta=3。否则的话Sta=1。怎么写?
主要是不知道怎么将Status不相同的几种商品赋一个新状态。


...全文
297 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
白发程序猿 2006-10-16
  • 打赏
  • 举报
回复
select bra,sec,num,case when cnt=1 then case when status=0 then 0 when status=1 then 4 when status=9 then 3 else 1 end else 1 end as sta
from (select bra,sec,num,count(*) as cnt,max(status) as status
from tablename
group by bra,sec,num)
白发程序猿 2006-10-16
  • 打赏
  • 举报
回复
想了一下,上面的是錯的
改為:
select bra,sec,num,case when cnt=1 then case when status=0 then 0 when status=1 then 4 when status=9 then 3 else 1 end else 1 end as sta
from (select bra,sec,num,count(*) as cnt,max(status) as status
from (select distinct bra,sec,num,status from tablename)
group by bra,sec,num)
这里有一个问题:就是num是怎样得到的,原表的num是jan的吗?结果集的num是求和的吗?如是的话,那上面的也不对,要改为:
select bra,sec,num,case when cnt=1 then case when status=0 then 0 when status=1 then 4 when status=9 then 3 else 1 end else 1 end as sta
from (select bra,sec,sum(num) as num,count(*) as cnt,max(status) as status
from (select bra,sec,sum(num) as num,status
from tablename
group by bra,sec,status)
group by bra,sec)
bwguang 2006-10-13
  • 打赏
  • 举报
回复
晚了,
明天再上。
bwguang 2006-10-13
  • 打赏
  • 举报
回复
我说的是简化的。
下面的是具体的做法。
我的想法是先找出同一个SEC和BRA 中Status=0的行 。作为集A1
然后求Status=1,9 的。分别为A2,A3
最后就是
然后再就是求出4楼第二个结果。但是第四列全部赋值1。作为集A4
让后将A4中SEC和BRA列与A1,A2,A3 重复的部分换成A1,A2,A3。
但是不知道怎么把A1-A3 中的数据替换到A4 。我刚学SQL .这个题目对我有点难。
select
distinct cart.SectionCode,
IP.SectionName,
cart.BranchCode,
UU.BranchName,
cart.Alloc,
status = 0
-- cart.PickingStatus
from
(select
DD.SectionCode
,DD.BranchCode
,sum(DD.AllocQty) as Alloc
-- ,DD.PickingStatus
from
WrkDpsDivisionCart as DD
where
DD.warehousecode = 995
and DD.pickingstatus = 0
-- and not
-- DD.pickingstatus = 9
-- and DD.pickingstatus = 1
-- and DD.pickingstatus = 3
-- Dd.Reqdate = @ReqDate
Group by
DD.SectionCode,
DD.BranchCode
-- ,DD.PickingStatus
)as cart
inner join
TMCSystem.dbo.DPSImportantProducts as IP
on
cart.SectionCode = IP.SectionCode
inner join
TMCSystem.dbo.TMC_Use_Branch as UU
on
cart.BranchCode = UU.BranchCD
bwguang 2006-10-13
  • 打赏
  • 举报
回复
SEC BRA Num Status
53 5 4 0
53 6 1 0
53 8 1 0
53 9 1 0
53 10 3 0
59 5 1 0
59 8 1 0
64 5 9 0
64 6 9 0
64 8 7 0
64 9 7 0
64 10 5 0
53 6 3 1
53 8 3 1
53 9 3 1
53 10 1 1
59 6 1 1
64 5 3 1
64 6 1 1
64 10 2 1
59 9 1 3
59 10 1 3
64 6 2 9
64 8 5 9
64 9 5 9
64 10 5 9
这是之前存储过程运行后的结果。
要求是这样的。
53 5 4
59 5 1
64 5 12
53 6 4
59 6 1
64 6 12
53 8 4
59 8 1
64 8 12
53 9 4
59 9 1
64 9 12
53 10 4
59 10 1
64 10 12
这是要求的结果。第四列就是状态字Sta。
状态字的定义是,如果每个BRA和SEC中的商品JAN的Status 全部为0 状态字Sta=0 ,全部为1 Sta=4,全部为 9 Sta=3。否则的话Sta=1
xiaoku 2006-10-13
  • 打赏
  • 举报
回复
不明白你说什么?
xyxfly 2006-10-13
  • 打赏
  • 举报
回复
case when
你这个描述有点乱


给点数据吧 还有结果

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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