第一个语句 字段三为要求更新的结果
字段一 字段二 字段三 (更新字段三的计算方式)
A 10 10 为A字段上一行字段三的值加上A字段本行字段二的值
B 20 20 为B字段上一行字段三的值加上B字段本行字段二的值
A 30 10+30 为A字段上一行字段三的值加上A字段本行字段二的值
B 40 20+40 为B字段上一行字段三的值加上B字段本行字段二的值
A 50 10+30+50 为A字段上一行字段三的值加上A字段本行字段二的值
B 60 20+40+60 为B字段上一行字段三的值加上B字段本行字段二的值
.......
第二个语句 字段三为要求更新的结果
字段一 字段二 字段三 (更新字段三的计算方式)
A 10 10+30+50 为A的字段二总和
B 20 20+40+60 为B的字段二总和
A 30 10+30+50 为A的字段二总和
B 40 20+40+60 为B的字段二总和
A 50 10+30+50 为A的字段二总和
B 60 20+40+60 为B的字段二总和
........这两个需求能分别各用一个语句实现吗?
字段一 字段二 字段三 (更新字段三的计算方式)
A 10 10 为A字段上一行字段三的值加上A字段本行字段二的值
B 20 20 为B字段上一行字段三的值加上B字段本行字段二的值
A 30 10+30 为A字段上一行字段三的值加上A字段本行字段二的值
B 40 20+40 为B字段上一行字段三的值加上B字段本行字段二的值
A 50 10+30+50 为A字段上一行字段三的值加上A字段本行字段二的值
B 60 20+40+60 为B字段上一行字段三的值加上B字段本行字段二的值
.......
第二个语句 字段三为要求更新的结果
字段一 字段二 字段三 (更新字段三的计算方式)
A 10 10+30+50 为A的字段二总和
B 20 20+40+60 为B的字段二总和
A 30 10+30+50 为A的字段二总和
B 40 20+40+60 为B的字段二总和
A 50 10+30+50 为A的字段二总和
B 60 20+40+60 为B的字段二总和
........这两个需求能分别各用一个语句实现吗?
参考这个有以下表结构(tablename) ID UserID thedate thetime(单位:分钟)
-------------------------------------------------------------------
1 AL000984 1899-12-30 10:41:00
5 AL000984 1899-12-30 10:42:00
6 AL000984 1899-12-30 10:46:00
14 AL000984 1899-12-30 10:57:00 17 BL000985 1899-12-30 11:06:00
18 BL000985 1899-12-30 11:14:00
44 BL000985 1899-12-30 11:24:00
50 BL000985 1899-12-30 11:36:00
51 BL000985 1899-12-30 11:46:00
tablename中有多个用户(现在示例中仅列出两个用户AL000984和BL000985),thedate字段是一个日期时间字段,是某业务操作时间,有先后顺序,现在我想
根据用户来取得相邻两记录相差的时间值,并写入thetime字段内,单位为分钟,
即要得下面的结果: ID UserID thedate thetime(单位:分钟)
-------------------------------------------------------------------
1 AL000984 1899-12-30 10:41:00 0
5 AL000984 1899-12-30 10:42:00 1
6 AL000984 1899-12-30 10:46:00 4
14 AL000984 1899-12-30 10:57:00 11 17 BL000985 1899-12-30 11:06:00 0
18 BL000985 1899-12-30 11:14:00 8
44 BL000985 1899-12-30 11:24:00 10
50 BL000985 1899-12-30 11:36:00 12
51 BL000985 1899-12-30 11:46:00 10 希望用一条SQL语句解决,谢谢大侠门!本人对在一个表中进行不同记录之间的比较很少做,不知从何下手
create table tb(ID int,UserID varchar(10),thedate varchar(10),thetime varchar(10))
insert into tb values(1 ,'AL000984', '1899-12-30', '10:41:00')
insert into tb values(5 ,'AL000984', '1899-12-30', '10:42:00')
insert into tb values(6 ,'AL000984', '1899-12-30', '10:46:00')
insert into tb values(14 ,'AL000984', '1899-12-30', '10:57:00')
insert into tb values(17 ,'BL000985', '1899-12-30', '11:06:00')
insert into tb values(18 ,'BL000985', '1899-12-30', '11:14:00')
insert into tb values(44 ,'BL000985', '1899-12-30', '11:24:00')
insert into tb values(50 ,'BL000985', '1899-12-30', '11:36:00')
insert into tb values(51 ,'BL000985', '1899-12-30', '11:46:00')
go[code=SQL]
create table tb(ID int,UserID varchar(10),thedatetime datetime)
insert into tb values(1 ,'AL000984', '1899-12-30 10:41:00')
insert into tb values(5 ,'AL000984', '1899-12-30 10:42:00')
insert into tb values(6 ,'AL000984', '1899-12-30 10:46:00')
insert into tb values(14 ,'AL000984', '1899-12-30 10:57:00')
insert into tb values(17 ,'BL000985', '1899-12-30 11:06:00')
insert into tb values(18 ,'BL000985', '1899-12-30 11:14:00')
insert into tb values(44 ,'BL000985', '1899-12-30 11:24:00')
insert into tb values(50 ,'BL000985', '1899-12-30 11:36:00')
insert into tb values(51 ,'BL000985', '1899-12-30 11:46:00')
go
select *,[minute] = isnull(datediff(minute,(select max(thedatetime) from tb where UserID=a.UserID and thedatetime <a.thedatetime),a.thedatetime),0) from tb a drop table tb/*
ID UserID thedatetime minute
----------- ---------- ------------------------------------------------------ -----------
1 AL000984 1899-12-30 10:41:00.000 0
5 AL000984 1899-12-30 10:42:00.000 1
6 AL000984 1899-12-30 10:46:00.000 4
14 AL000984 1899-12-30 10:57:00.000 11
17 BL000985 1899-12-30 11:06:00.000 0
18 BL000985 1899-12-30 11:14:00.000 8
44 BL000985 1899-12-30 11:24:00.000 10
50 BL000985 1899-12-30 11:36:00.000 12
51 BL000985 1899-12-30 11:46:00.000 10(所影响的行数为 9 行)
*/select t.id,t.UserID,t.thedate,t.thetime,[minute] = 0 from
(
select px=(select count(1) from tb where UserID=a.UserID and cast(thedate + ' ' + thetime as datetime)<cast(a.thedate + ' ' + a.thetime as datetime))+1 , * from tb a
) t where px = 1
union all
select n.id,n.UserID,n.thedate,n.thetime,[minute]=datediff(minute,cast(m.thedate + ' ' + m.thetime as datetime),cast(n.thedate + ' ' + n.thetime as datetime)) from
(
select px=(select count(1) from tb where UserID=a.UserID and cast(thedate + ' ' + thetime as datetime)<cast(a.thedate + ' ' + a.thetime as datetime))+1 , * from tb a
) m,
(
select px=(select count(1) from tb where UserID=a.UserID and cast(thedate + ' ' + thetime as datetime)<cast(a.thedate + ' ' + a.thetime as datetime))+1 , * from tb a
) n
where m.UserID = n.UserID and m.px = n.px - 1
order by t.userid,t.thedate,t.thetimedrop table tb/*
id UserID thedate thetime minute
----------- ---------- ---------- ---------- -----------
1 AL000984 1899-12-30 10:41:00 0
5 AL000984 1899-12-30 10:42:00 1
6 AL000984 1899-12-30 10:46:00 4
14 AL000984 1899-12-30 10:57:00 11
17 BL000985 1899-12-30 11:06:00 0
18 BL000985 1899-12-30 11:14:00 8
44 BL000985 1899-12-30 11:24:00 10
50 BL000985 1899-12-30 11:36:00 12
51 BL000985 1899-12-30 11:46:00 10
*/
--把thedate ,thetime合起来做为一个时间字段更好
create table tb(ID int,UserID varchar(10),thedatetime datetime)
insert into tb values(1 ,'AL000984', '1899-12-30 10:41:00')
insert into tb values(5 ,'AL000984', '1899-12-30 10:42:00')
insert into tb values(6 ,'AL000984', '1899-12-30 10:46:00')
insert into tb values(14 ,'AL000984', '1899-12-30 10:57:00')
insert into tb values(17 ,'BL000985', '1899-12-30 11:06:00')
insert into tb values(18 ,'BL000985', '1899-12-30 11:14:00')
insert into tb values(44 ,'BL000985', '1899-12-30 11:24:00')
insert into tb values(50 ,'BL000985', '1899-12-30 11:36:00')
insert into tb values(51 ,'BL000985', '1899-12-30 11:46:00')
goselect t.id,t.UserID,t.thedatetime,[minute] = 0 from
(
select px=(select count(1) from tb where UserID=a.UserID and thedatetime<a.thedatetime)+1 , * from tb a
) t where px = 1
union all
select n.id,n.UserID,n.thedatetime,[minute]=datediff(minute,m.thedatetime,n.thedatetime) from
(
select px=(select count(1) from tb where UserID=a.UserID and thedatetime<a.thedatetime)+1 , * from tb a
) m,
(
select px=(select count(1) from tb where UserID=a.UserID and thedatetime<a.thedatetime)+1 , * from tb a
) n
where m.UserID = n.UserID and m.px = n.px - 1
order by t.userid,t.thedatetimedrop table tb/*
id UserID thedatetime minute
----------- ---------- ------------------------------------------------------ -----------
1 AL000984 1899-12-30 10:41:00.000 0
5 AL000984 1899-12-30 10:42:00.000 1
6 AL000984 1899-12-30 10:46:00.000 4
14 AL000984 1899-12-30 10:57:00.000 11
17 BL000985 1899-12-30 11:06:00.000 0
18 BL000985 1899-12-30 11:14:00.000 8
44 BL000985 1899-12-30 11:24:00.000 10
50 BL000985 1899-12-30 11:36:00.000 12
51 BL000985 1899-12-30 11:46:00.000 10(所影响的行数为 9 行)
*/[/code]
--环境
create table tab1
(
a varchar(1),
b int,
c int
)insert into tab1(a,b) select 'A', 10
insert into tab1(a,b) select 'B', 20
insert into tab1(a,b) select 'A', 30
insert into tab1(a,b) select 'B', 40
insert into tab1(a,b) select 'A', 50
insert into tab1(a,b) select 'B', 60--1.如果楼主的表里存在这样的字段就不用加辅助列了
--增加辅助列
alter table tab1 add id int identity(1,1)--更新
update tab1 set c = tab2.c
from
(select a,b,c = (select sum(isnull(b,0)) from tab1 where a = tt.a and id <= tt.id),id
from tab1 tt)tab2
where tab1.id = tab2.id--删除辅助列
alter table tab1 drop column id--结果
/*
a b c
---- ----------- -----------
A 10 10
B 20 20
A 30 40
B 40 60
A 50 90
B 60 120(6 row(s) affected)
*/--2.
update t1 set t1.c = t2.c
from tab1 t1,(select a,sum(b) as c from tab1 group by a)t2
where t1.a = t2.a--结果
/*
a b c
---- ----------- -----------
A 10 90
B 20 120
A 30 90
B 40 120
A 50 90
B 60 120(6 row(s) affected)
*/
字段一 字段二 字段三 (更新字段三的计算方式)
A 10 10+30+50 为A的字段二总和
B 20 20+40+60 为B的字段二总和
A 30 10+30+50 为A的字段二总和
B 40 20+40+60 为B的字段二总和
A 50 10+30+50 为A的字段二总和
B 60 20+40+60 为B的字段二总和
update tb
set 字段三 = m.字段二
from tb t , (select 字段一,sum(字段二) 字段二 from tb group by 字段一) m
where t.字段一 = m.字段一
DECLARE @a TABLE(a VARCHAR(10),b INT,c INT)
INSERT @a SELECT 'A',10,NULL
UNION ALL SELECT 'B',20,NULL
UNION ALL SELECT 'A',30,NULL
UNION ALL SELECT 'B',40,NULL
UNION ALL SELECT 'A',50,NULL
UNION ALL SELECT 'B',60,NULL --1.
PRINT '--1--'
DECLARE @b TABLE(id INT IDENTITY(1,1),a VARCHAR(10),b INT,c INT)
INSERT @b SELECT * FROM @a
SELECT a,b,c=(SELECT sum(b) FROM @b WHERE a=b.a AND id<=b.id) FROM @b b
--2.
PRINT '--2--'
SELECT a,b,c=(SELECT sum(b) FROM @a WHERE a=b.a) FROM @a b
--result
/*--1--
a b c
---------- ----------- -----------
A 10 10
B 20 20
A 30 40
B 40 60
A 50 90
B 60 120--2--
a b c
---------- ----------- -----------
A 10 90
B 20 120
A 30 90
B 40 120
A 50 90
B 60 120*/
--例子数据
declare @tab1 table (a varchar(1),b int,c int)
insert into @tab1(a,b) select 'A', 10
insert into @tab1(a,b) select 'B', 20
insert into @tab1(a,b) select 'A', 30
insert into @tab1(a,b) select 'B', 40
insert into @tab1(a,b) select 'A', 50
insert into @tab1(a,b) select 'B', 60
--你要的语句
update a set c = b+isnull((select max(c) from @tab1 where a.a=a ),0) from @tab1 a
--检查结果
select * from @tab1
/*
a b c
---- ----------- -----------
A 10 10
B 20 20
A 30 30
B 40 40
A 50 50
B 60 60
*/
1.
CREATE TABLE tp
(
id int identity(1,1) not null,one char(2),tow int,three int
)
create trigger tri_tp_insert
on tp
for insert as
begin
declare @one char(2),@three int,@id int
select @one=one,@id=id from inserted
select @three=sum(tow) from tp where one=@one
update tp set three=@three where id=@id
end
insert tp(one,tow)values('a',100)select * from tp2.create table tp2
(
id int identity(1,1) not null,one char(2),tow int,three int
)create trigger tri_tp2_insert
on tp2
for insert asbegin
declare @one char(2),@two int
select @one=one from inserted
select @two=sum(tow) from tp2 where one=@one
update tp2 set three=@two where one=@one
endinsert tp2(one,tow)values('a',100)select * from tp2
create table tab1(a varchar(10),b int,c int)
insert into tab1(a,b) select 'A', 10
insert into tab1(a,b) select 'B', 20
insert into tab1(a,b) select 'A', 30
insert into tab1(a,b) select 'B', 40
insert into tab1(a,b) select 'A', 50
insert into tab1(a,b) select 'B', 60
select id=identity(int,1,1),* into # from tab1
-----1、
select a.a,a.b,c=a.b+isnull(b.b,0)+isnull(c.b,0) from # a
left join # b on a.id=b.id+2
left join # c on a.id=c.id+4
-----2、
select a.a,a.b,b.c from # a,(select id%2 id,sum(b)c from # group by id%2) b
where a.id%2=b.id order by a.id