22,294
社区成员
发帖
与我相关
我的任务
分享--sorry 分组弄错
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-04-01 16:57:21
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4053.00 (Intel X86)
-- May 26 2009 14:24:20
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[uid] int,[objid] int,[dowhat] varchar(4),[score] int,[createtime] datetime)
insert [tb]
select 1,1,1,'评论',0,'2010-11-11 11:11:11' union all
select 2,1,1,'评论',0,'2010-01-11 11:11:11' union all
select 3,1,2,'评论',0,'2010-02-11 11:11:11' union all
select 4,2,1,'评论',0,'2010-05-11 11:11:11' union all
select 5,2,2,'收藏',0,'2010-03-11 11:11:11' union all
select 6,2,2,'评论',0,'2010-04-11 11:11:11' union all
select 7,2,1,'评论',0,'2010-12-11 11:11:11' union all
select 8,1,2,'评论',0,'2010-06-11 11:11:11' union all
select 9,2,2,'评论',0,'2010-07-11 11:11:11'
--------------开始查询--------------------------
update
b
set
score=2
from
tb a,
(select id0=row_number()over(partition by uid,objid order by getdate()),* from tb where dowhat='评论')b
where
a.uid=b.uid
and
b.id0=1
select * from tb
----------------结果----------------------------
/* id uid objid dowhat score createtime
----------- ----------- ----------- ------ ----------- -----------------------
1 1 1 评论 2 2010-11-11 11:11:11.000
2 1 1 评论 0 2010-01-11 11:11:11.000
3 1 2 评论 2 2010-02-11 11:11:11.000
4 2 1 评论 0 2010-05-11 11:11:11.000
5 2 2 收藏 0 2010-03-11 11:11:11.000
6 2 2 评论 2 2010-04-11 11:11:11.000
7 2 1 评论 2 2010-12-11 11:11:11.000
8 1 2 评论 0 2010-06-11 11:11:11.000
9 2 2 评论 0 2010-07-11 11:11:11.000
(9 行受影响)
*/update tb
set score = 2
from tb t
where dowhat = '评论' and createtime = (select min(createtime) from tb where uid = t.uid and objid = t.objid and dowhat = '评论')----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-04-01 16:57:21
-- Verstion:
-- Microsoft SQL Server 2005 - 9.00.4053.00 (Intel X86)
-- May 26 2009 14:24:20
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[uid] int,[objid] int,[dowhat] varchar(4),[score] int,[createtime] datetime)
insert [tb]
select 1,1,1,'评论',0,'2010-11-11 11:11:11' union all
select 2,1,1,'评论',0,'2010-01-11 11:11:11' union all
select 3,1,2,'评论',0,'2010-02-11 11:11:11' union all
select 4,2,1,'评论',0,'2010-05-11 11:11:11' union all
select 5,2,2,'收藏',0,'2010-03-11 11:11:11' union all
select 6,2,2,'评论',0,'2010-04-11 11:11:11' union all
select 7,2,1,'评论',0,'2010-12-11 11:11:11' union all
select 8,1,2,'评论',0,'2010-06-11 11:11:11' union all
select 9,2,2,'评论',0,'2010-07-11 11:11:11'
--------------开始查询--------------------------
update
b
set
score=2
from
tb a,
(select id0=row_number()over(partition by uid order by dowhat),* from tb where dowhat='评论')b
where
a.uid=b.uid
and
b.id0=1
select * from tb
----------------结果----------------------------
/* id uid objid dowhat score createtime
----------- ----------- ----------- ------ ----------- -----------------------
1 1 1 评论 2 2010-11-11 11:11:11.000
2 1 1 评论 0 2010-01-11 11:11:11.000
3 1 2 评论 0 2010-02-11 11:11:11.000
4 2 1 评论 0 2010-05-11 11:11:11.000
5 2 2 收藏 0 2010-03-11 11:11:11.000
6 2 2 评论 0 2010-04-11 11:11:11.000
7 2 1 评论 0 2010-12-11 11:11:11.000
8 1 2 评论 0 2010-06-11 11:11:11.000
9 2 2 评论 2 2010-07-11 11:11:11.000
(9 行受影响)
*/[code=SQL]update
a
set
score=2
from
tb a,
(select id=row_number()over(partiotion by uid order by dowhat),* from tb where dowhat='评论')b
where
a.uid=b.uid
and
b.uid=1
[/code]update
a
set
score=2
from
tb a,
(select id=row_number()over(partiotion by uid order by dowhat),* from tb)b
where
a.uid=b.uid
and
b.uid=1