用一條SQL語句解決

dnvodcwan 2003-08-19 02:30:04
Field1 Field2 update-->Field1 Field2
A null A 1
A null A 2
B Null B 1
C Null C 1
A Null A 3
B Null B 2
C Null C 2
...全文
52 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Stone444 2003-09-07
  • 打赏
  • 举报
回复
create table test ( a varchar(10),
b int,
c int identity(1,1))

insert into test(a) values('A')
insert into test(a) values('A')
insert into test(a) values('B')
insert into test(a) values('C')
insert into test(a) values('A')
insert into test(a) values('B')

update test set test.b=
(
select distinct temp.t2 from
(
select a.a,b.c,count(a.a) as t2 from test a,test b
where a.a=b.a and a.c<=b.c group by a.a,b.c
) as temp
where temp.a=test.a and temp.c=test.c
)
GO
zarge 2003-09-07
  • 打赏
  • 举报
回复
update test set count=con.bb(
select sum(1)as bb,max(a.uid) as uu from test a ,test b where a.test_id=b.test_id and a.uid>=b.uid group by a.test_id,a.uid)con
where test.uid=con.uu order by test_id,con.bb

如果表有id的话,这样写就可以了,但是后面的order by test_id,con.bb应该去掉
maoyesky 2003-09-06
  • 打赏
  • 举报
回复
create table jj (a varchar(10),b int)

insert into jj (a) values ('A')
insert into jj (a) values ('A')
insert into jj (a) values ('B')
insert into jj (a) values ('C')
insert into jj (a) values ('A')
insert into jj (a) values ('B')
insert into jj (a) values ('C')

create table kk (a varchar(10),b int,c int identity(1,1))

Insert into kk select * from jj

update kk set kk.b=(select distinct temp.t2 from (select mao.a,kk.c,t1,count(kk.a) as t2
from (select a,count(a) as t1,c from kk group by a,c) as mao,kk
where mao.a=kk.a and mao.c <= kk.c group by mao.a,mao.t1,kk.c) as temp where temp.a=kk.a and temp.c=kk.c)

--如下是结果:
select * from jj
dnvodcwan 2003-09-05
  • 打赏
  • 举报
回复
問題已解決了
create test (
test_id varchar (10) not null,
test_name varchar (10) not null,
count numeric (28,2) ,
uid numeric (28,0) identity (1,1)
go




update test set count=con.bb(
select sum(1)as bb,max(a.uid) as uu from test a ,test b where a.test_id=b.test_id and a.uid>=b.uid group by a.test_id,a.uid)con
where test.uid=con.uu order by test_id,con.bb
dnvodcwan 2003-08-19
  • 打赏
  • 举报
回复
不可能用變量,再想想吧
dnvodcwan 2003-08-19
  • 打赏
  • 举报
回复
失敗的應聘者
CrazyFor 2003-08-19
  • 打赏
  • 举报
回复
你的field1一共有多少种类型,就定义多少个变量:
declar @a int,@b int,@c int
set @a=1
set @b=1
set @c=1
update 表 set
field2 =case when field1='A' then @a
when field1='B' then @b
when field1='C' then @c end
,@A=case when field1='A' then @a+1 else @a end
,@b=case when field1='B' then @b+1 else @b end
,@c=case when field1='C' then @c+1 else @c end

select * from 表
愉快的登山者 2003-08-19
  • 打赏
  • 举报
回复
用一條SQL語句解決不了。

愉快的登山者


◢◣◢◣◢◣
愉快的登山者 2003-08-19
  • 打赏
  • 举报
回复
用一條SQL語句解決不了。

愉快的登山者


◢◣◢◣◢◣

27,579

社区成员

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

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