请大家帮帮忙,关于存储过程的问题!!

qingyun67 2005-01-12 11:39:49
,现在有一个表,表中有编号,日期,数量,补助等字段,表中一个编号对应很多日期,现在我想要的结果是,对于一个编号,在它对应的日期中,进行判断,日期连续三天以上的,则对应记录补助=数量*0.2,这样的存储过程怎么实现呢?请大侠们帮帮忙!!!!!!
比如:
id 编号 日期 数量 补助
1 001 2005-1-1 2
2 001 2005-1-3 3
3 001 2005-1-4 2
4 001 2005-1-5 3
5 001 2005-1-6 2
6 002 2005-1-2 5
7 002 2005-1-3 5
8 002 2005-1-3 4
9 002 2005-1-4 5
想实现的结果:
id 编号 日期 数量 补助
1 001 2005-1-1 2 0
2 001 2005-1-3 3 0.6
3 001 2005-1-4 2 0.4
4 001 2005-1-5 3 0.6
5 001 2005-1-6 2 0.4
6 002 2005-1-2 5 1
7 002 2005-1-3 5 1
8 002 2005-1-3 4 0.8
9 002 2005-1-4 5 1
10 002 2005-1-6 2 0
...全文
116 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qingyun67 2005-01-13
  • 打赏
  • 举报
回复
这个日期连续几天是可以别动的,比如说可以定是连续5天以上的!!!!!!!
qingyun67 2005-01-12
  • 打赏
  • 举报
回复
to:zjcxc(邹建) ,
第一句的错误提示:
服务器: 消息 107,级别 16,状态 2,行 2
列前缀 'a' 与查询中所用的表名或别名不匹配。
qingyun67 2005-01-12
  • 打赏
  • 举报
回复
to:zjcxc(邹建) ,这个日期连续几天是可以别动的,比如说可以定是连续5天以上的,
zjcxc 元老 2005-01-12
  • 打赏
  • 举报
回复
--更新
update a set
补助=case when b.编号 is null then 0 else a.数量*.2 end
from 表 a left join(
select distinct a.编号
from 表 aa,表 bb,表 cc
where aa.编号=bb.编号
and aa.编号=cc.编号
and aa.日期=bb.日期-1
and cc.日期=bb.日期+1
)b on a.编号=b.编号
zjcxc 元老 2005-01-12
  • 打赏
  • 举报
回复
select a.id,a.编号,a.日期,a.数量
,补助=case when b.编号 is null then 0 else a.数量*.2 end
from 表 a left join(
select distinct a.编号
from 表 aa,表 bb,表 cc
where aa.编号=bb.编号
and aa.编号=cc.编号
and aa.日期=bb.日期-1
and cc.日期=bb.日期+1
)b on a.编号=b.编号

Softlee81307 2005-01-12
  • 打赏
  • 举报
回复
Create Table test10(
id1 int,
idno varchar(3),
iddate datetime,
idqty int,
idpu decimal(8,1)
)
insert into test10(id1,idno,iddate,idqty)
select 1 ,'001', '2005-1-1', 2 union all
select 2 ,'001', '2005-1-3', 3 union all
select 3 ,'001', '2005-1-4', 2 union all
select 4 ,'001', '2005-1-5', 3 union all
select 5 ,'001', '2005-1-6', 2 union all
select 6 ,'002', '2005-1-2', 5 union all
select 7 ,'002', '2005-1-3', 5 union all
select 8 ,'002', '2005-1-3', 4 union all
select 9 ,'002', '2005-1-4', 5 union all

---------------建函數-------------------
create function getqty(@k varchar(3),@i datetime)
returns decimal(8,1)
as
begin
declare @ppp int
declare @s decimal(8,1)
set @s=0
set @ppp=0
select @ppp= count(*) from (
select idno,cs from (
select c.idno,c.iddate as cs,a.iddate as ds,b.iddate as dd from test10 c inner join
(select distinct * from test10) a on(a.iddate=dateadd(dd,-1,c.iddate) and a.idno=c.idno)
inner join (select distinct * from test10) b on(b.iddate=dateadd(dd,1,c.iddate) and b.idno=c.idno)
) gh
union
select idno,ds from (
select c.idno,c.iddate as cs,a.iddate as ds,b.iddate as dd from test10 c inner join
(select distinct * from test10) a on(a.iddate=dateadd(dd,-1,c.iddate) and a.idno=c.idno)
inner join (select distinct * from test10) b on(b.iddate=dateadd(dd,1,c.iddate) and b.idno=c.idno)
) gg
union
select idno,dd from (
select c.idno,c.iddate as cs,a.iddate as ds,b.iddate as dd from test10 c inner join
(select distinct * from test10) a on(a.iddate=dateadd(dd,-1,c.iddate) and a.idno=c.idno)
inner join (select distinct * from test10) b on(b.iddate=dateadd(dd,1,c.iddate) and b.idno=c.idno)
) gd ) gg where idno=@k and cs=@i
if @ppp>0
set @s=0.2
return(@s)


end
--------------------------------------------------------------
---------------語句-----------------
select id1,idno,iddate,idqty,idpu=idqty*dbo.getqty(idno,iddate) from test10

34,590

社区成员

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

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