急!求一个UPdate的语句?

anxl 2004-06-17 11:29:09
有三个字段其中“B编号”字段的编号规则为根据“A类别”字段每个类别从1开始计数。并且每月都按照规则从新计数。现在我把“A类别”全部更新为“1”要求“B编号”按照日期排序从新计数而且到下个月再次从新计数(更新后结果参照下面)我该如何写UPDATE的语句可以一次完成?
A类别 B编号 C日期
1 001 2004-06-01
1 002 2004-06-02
1 003 2004-06-03
2 001 2004-06-04
2 002 2004-06-05
3 001 2004-06-06
3 002 2004-06-07
3 003 2004-06-08
………………………………………………
1 001 2004-07-01
1 002 2004-07-02
1 003 2004-07-03
2 001 2004-07-04
2 002 2004-07-05
3 001 2004-07-06
3 002 2004-07-07
3 003 2004-07-08
更新后
1 001 2004-06-01
1 002 2004-06-02
1 003 2004-06-03
1 004 2004-06-04
1 005 2004-06-05
1 006 2004-06-06
1 007 2004-06-07
1 008 2004-06-08
………………………………………………
1 001 2004-07-01
1 002 2004-07-02
1 003 2004-07-03
1 004 2004-07-04
1 005 2004-07-05
1 006 2004-07-06
1 007 2004-07-07
1 008 2004-07-08


...全文
108 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjmym 2004-06-22
  • 打赏
  • 举报
回复
改为
update 表 set A类别=1,B编号=right(1000+(
select count(*) from 表
where cast(C日期 as char(20))+D行号<=cast(a.C日期 as char(20))+a.D行号
and datediff(month,C日期,a.C日期)=0
),3)
from 表 a
就可以了
zjmym 2004-06-22
  • 打赏
  • 举报
回复
全是偶数是因为重复字段
2004-06-01 00:00:00.000
2004-06-01 00:00:00.000
zjmym 2004-06-22
  • 打赏
  • 举报
回复
right(1000+(select count(*) from 表 where C日期<=a.C日期 and datediff(month,C日期,a.C日期)=0),3)

(select count(*) from 表 where C日期<=a.C日期 and datediff(month,C日期,a.C日期)=0)
B编号字段在各个月中的编号。
结果为1,2,。。10,11。。。n
1000+这个结果得到结果2为4位表示的编号
结果为1001,1002。。1010,1011。。(1000+n)
right(结果2,3)表示取右边3位
结果为001,002。。010,011。。0xx
anxl 2004-06-22
  • 打赏
  • 举报
回复
非常感谢 zjcxc(邹建)!如果我的测试数据如下:为什么Update后B编号字段全部为双数!
如何修改可以按顺序排号,另外“B编号=right(1000+(select count(*) from 表 where C日期<=a.C日期 and datediff(month,C日期,a.C日期)=0),3)”是什么意思?

create table 表(A类别 int,B编号 char(3),D行号 char(1),C日期 datetime)
insert 表 select 1,'01','1','2004-06-01'
union all select 1,'01','2','2004-06-01'
union all select 1,'02','1','2004-06-02'
union all select 1,'02','2','2004-06-02'
union all select 1,'03','1','2004-06-03'
union all select 1,'03','2','2004-06-03'
union all select 2,'04','1','2004-06-04'
union all select 2,'04','2','2004-06-04'
union all select 1,'01','1','2004-07-01'
union all select 1,'01','2','2004-07-01'
union all select 1,'02','1','2004-07-02'
union all select 1,'02','2','2004-07-02'
union all select 1,'03','1','2004-07-03'
union all select 1,'03','2','2004-07-03'
union all select 2,'04','1','2004-07-04'
union all select 1,'04','2','2004-07-04'

/*--测试结果

A类别 B编号 D行号 C日期
----------- ---- ----------------------------
1 02 1 2004-06-01 00:00:00.000
1 02 2 2004-06-01 00:00:00.000
1 04 1 2004-06-02 00:00:00.000
1 04 2 2004-06-02 00:00:00.000
1 06 1 2004-06-03 00:00:00.000
1 06 2 2004-06-03 00:00:00.000
1 08 1 2004-06-04 00:00:00.000
1 08 2 2004-06-04 00:00:00.000
1 02 1 2004-07-01 00:00:00.000
1 02 2 2004-07-01 00:00:00.000
1 04 1 2004-07-02 00:00:00.000
1 04 2 2004-07-02 00:00:00.000
1 06 1 2004-07-03 00:00:00.000
1 06 2 2004-07-03 00:00:00.000
1 08 1 2004-07-04 00:00:00.000
1 08 2 2004-07-04 00:00:00.000

yeeshengwei 2004-06-22
  • 打赏
  • 举报
回复
up!
anxl 2004-06-22
  • 打赏
  • 举报
回复
非常感谢!试下
zjcxc 2004-06-20
  • 打赏
  • 举报
回复
--测试

--测试数据
create table 表(A类别 int,B编号 char(3),C日期 datetime)
insert 表 select 1,'001','2004-06-01'
union all select 1,'002','2004-06-02'
union all select 1,'003','2004-06-03'
union all select 2,'001','2004-06-04'
union all select 2,'002','2004-06-05'
union all select 3,'001','2004-06-06'
union all select 3,'002','2004-06-07'
union all select 3,'003','2004-06-08'
union all select 1,'001','2004-07-01'
union all select 1,'002','2004-07-02'
union all select 1,'003','2004-07-03'
union all select 2,'001','2004-07-04'
union all select 2,'002','2004-07-05'
union all select 3,'001','2004-07-06'
union all select 3,'002','2004-07-07'
union all select 3,'003','2004-07-08'
go

--更新
update 表 set A类别=1,B编号=right(1000+(
select count(*) from 表
where C日期<=a.C日期
and datediff(month,C日期,a.C日期)=0
),3)
from 表 a
go

--显示更新结果
select * from 表
go

--删除测试
drop table 表

/*--测试结果

A类别 B编号 C日期
----------- ---- ----------------------------
1 001 2004-06-01 00:00:00.000
1 002 2004-06-02 00:00:00.000
1 003 2004-06-03 00:00:00.000
1 004 2004-06-04 00:00:00.000
1 005 2004-06-05 00:00:00.000
1 006 2004-06-06 00:00:00.000
1 007 2004-06-07 00:00:00.000
1 008 2004-06-08 00:00:00.000
1 001 2004-07-01 00:00:00.000
1 002 2004-07-02 00:00:00.000
1 003 2004-07-03 00:00:00.000
1 004 2004-07-04 00:00:00.000
1 005 2004-07-05 00:00:00.000
1 006 2004-07-06 00:00:00.000
1 007 2004-07-07 00:00:00.000
1 008 2004-07-08 00:00:00.000

(所影响的行数为 16 行)
--*/
zjcxc 2004-06-20
  • 打赏
  • 举报
回复
--少写了一点
--更新
update 表 set A类别=1,B编号=right(1000+(
select count(*) from 表
where C日期<=a.C日期
and datediff(month,C日期,a.C日期)=0
),3)
from 表 a
zjcxc 2004-06-20
  • 打赏
  • 举报
回复
update 表 set A类别=1,B编号=right(1000+(
select count(*) from 表
where C日期<=a.C日期
and datediff(month,C日期,a.C日期)=0
),3)
anxl 2004-06-20
  • 打赏
  • 举报
回复
请大家不吝赐教
anxl 2004-06-19
  • 打赏
  • 举报
回复
没人会吗?

27,581

社区成员

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

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