字段A(VARCHAR) 字段B(INT) 字段C(INT) 字段D(DATETMIE) 字段E(INT)
A 2004 1 2004-6-10
A 2004 0 2004-6-11
A 2005 3 2004-6-12
A 2004 1 2004-6-13
A 2004 1 2004-6-14
A 2004 0 2004-6-15
A 2004 3 2004-6-16
b 2004 1 2004-6-17
b 2004 0 2004-6-18
b 2004 0 2004-6-19
b 2005 0 2004-6-20
b 2004 3 2004-6-21
b 2004 1 2004-6-22
b 2004 3 2004-6-23
b 2004 1 2004-6-24
b 2004 1 2004-6-25想写一个UPDATE 更新字段E 结果是 =字段A和=字段B 时间小于当前更新行的时间 的前五行数据行中 字段C >0 的数量 如不足五行则返回null
A 2004 1 2004-6-10
A 2004 0 2004-6-11
A 2005 3 2004-6-12
A 2004 1 2004-6-13
A 2004 1 2004-6-14
A 2004 0 2004-6-15
A 2004 3 2004-6-16
b 2004 1 2004-6-17
b 2004 0 2004-6-18
b 2004 0 2004-6-19
b 2005 0 2004-6-20
b 2004 3 2004-6-21
b 2004 1 2004-6-22
b 2004 3 2004-6-23
b 2004 1 2004-6-24
b 2004 1 2004-6-25想写一个UPDATE 更新字段E 结果是 =字段A和=字段B 时间小于当前更新行的时间 的前五行数据行中 字段C >0 的数量 如不足五行则返回null
解决方案 »
- 求一个更新多条语句的SQL
- SQL Server批量添加表的记录数据
- 可用服务器
- 求tsql语句:自然数连续id清零且保持原大小顺序
- 如何直接用sql语句导出某个表为excel格式???在线等……
- 不知道如何解决这个问题
- windows server 2003 简体中文企业版SP2 在安装SQL2000时为什么是自动退出安装
- 在sql 语句中,in 该怎么用啊?就是在用游标的时候,declare cursor for,语句中有 in 该怎么写啊?
- 如何在查询到的日期上上加30个工作日
- 怎样才能用SQL存储图片并用PB调用显示?请各位大虾指点~(百分请教)_
- 模糊查询如何实现的问题
- xp_cmdshell 局域网拷贝谢谢
第二行 小于他的时间的行不足五行则结果为NULL
第三行 小于他的时间的行不足五行则结果为NULL
第四行 小于他的时间的行不足五行则结果为NULL
第五行 小于他的时间的行不足五行则结果为NULL
第六行 小于他的时间的五行中 c>0 的数量为4 结果为4
第七行 小于他的时间的五行中 c>0的数量也是为4 结果为4
第八行 小于他的时间的行不足五行则结果为NULL
.....以此类推
create table t1(a varchar,b int,c int,d datetime,e int)
insert into t1(a,b,c,d)
select 'A',2004,1, '2004-6-10'
union all select 'A',2004,0,'2004-6-11'
union all select 'A', 2005, 3,'2004-6-12'
union all select 'A', 2004, 1,'2004-6-13'
union all select 'A', 2004, 1,'2004-6-14'
union all select 'A', 2004, 0,'2004-6-15'
union all select 'A', 2004, 3,'2004-6-16'
union all select 'b', 2004, 1,'2004-6-17'
union all select 'b', 2004, 0,'2004-6-18'
union all select 'b', 2004, 0,'2004-6-19'
union all select 'b', 2005, 0,'2004-6-20'
union all select 'b', 2004, 3,'2004-6-21'
union all select 'b', 2004, 1,'2004-6-22'
union all select 'b', 2004, 3,'2004-6-23'
union all select 'b', 2004, 1,'2004-6-24'
union all select 'b', 2004, 1,'2004-6-25'update t1
set e = case when (select count(*) from t1 a where a.d < t1.d) >= 5 then (select count(*) from t1 a where a.c > 0 and a.d < t1.d) end
from t1--输出结果
select * from t1
declare @t table(id int,A varchar(10),B int,C int ,D datetime,E int)
insert @t
select 1,'A',2004,1,'2004-6-10',0 union all
select 2,'A',2004,0,'2004-6-11',0 union all
select 3,'A',2005,3,'2004-6-12',0 union all
select 4,'A',2004,1,'2004-6-13',0 union all
select 5,'A',2004,1,'2004-6-14',0 union all
select 6,'A',2004,0,'2004-6-15',0 union all
select 7,'A',2004,3,'2004-6-16',0 union all
select 8,'B',2004,1,'2004-6-17',0 union all
select 9,'B',2004,0,'2004-6-18',0 union all
select 10,'B',2004,0,'2004-6-19',0 union all
select 11,'B',2005,0,'2004-6-20',0 union all
select 12,'B',2004,3,'2004-6-21',0 union all
select 13,'B',2004,1,'2004-6-22',0 union all
select 14,'B',2004,3,'2004-6-23',0 union all
select 15,'B',2004,1,'2004-6-24',0 union all
select 16,'B',2004,1,'2004-6-25',0
----更新
UPDATE x SET E = y.ct
FROM @t x left join
(SELECT id,ct = (select count(*) from @t where A = a.A and D< a.D and C>0) from @t a
where (select count(*) from @t where A = a.A and D < a.D and C> 0) >=4) y on x.id = y.id
----查看更新结果
select * from @t