select a.*
from 临时表 a left join(
select 疫苗编码,疫苗剂次=max(疫苗剂次)+1
from 临时表
where 状态='已种'
group by 疫苗编码
)b on a.疫苗编码=b.疫苗编码
where a.疫苗剂次<=isnull(b.疫苗剂次,(
select min(疫苗剂次) from 临时表
where 疫苗编码=a.疫苗编码))
from 临时表 a left join(
select 疫苗编码,疫苗剂次=max(疫苗剂次)+1
from 临时表
where 状态='已种'
group by 疫苗编码
)b on a.疫苗编码=b.疫苗编码
where a.疫苗剂次<=isnull(b.疫苗剂次,(
select min(疫苗剂次) from 临时表
where 疫苗编码=a.疫苗编码))
解决方案 »
- sqlserver远程服务器问题
- 该怎样写插入语句
- 如何对这样的字段分析后排序?
- 要买SQL SERVER 正版了,专业版和企业版哪个更适合我们哪?
- 各位大侠,请帮忙看一下,用语句实现记录添加唯一的序号,在线等
- 一个表里的统计,高手来。速度结贴!!!~
- 统计问题插入问题
- 如何在SQL2000中实现自动添加用户,并且自动设置权限呢?请各位高手出手!
- 疯子第请继续指教
- 怎样用SQL SERVER 7.0 备份单张表!
- SQL语句表达试 select * from table1 where username='xxx'.....马上结贴
- 由年龄分段统计想到的(参见 《 !!!!求教一个年龄段分组的sql 语句!! 》)
create table 临时表(疫苗编码 varchar(10),疫苗剂次 int,状态 varchar(4))
insert 临时表
select 'A', 1, '未种'
union select 'B', 1, '已种'
union select 'B', 2, '未种'
union select 'B', 3, '未种'
union select 'C', 1, '未种'
union select 'C', 2, '未种'
union select 'D', 1, '未种'
union select 'D', 2, '已种'
union select 'D', 3, '未种'
union select 'D', 4, '未种'
goselect a.*
from 临时表 a left join(
select 疫苗编码,疫苗剂次=max(疫苗剂次)+1
from 临时表
where 状态='已种'
group by 疫苗编码
)b on a.疫苗编码=b.疫苗编码
where a.疫苗剂次<=isnull(b.疫苗剂次,(
select min(疫苗剂次) from 临时表
where 疫苗编码=a.疫苗编码))
go--删除测试
drop table 临时表/*--测试结果
疫苗编码 疫苗剂次 状态
---------- ----------- ----
A 1 未种
B 1 已种
B 2 未种
C 1 未种
D 1 未种
D 2 已种
D 3 未种(所影响的行数为 7 行)
--*/
select min(疫苗剂次) from 临时表
where 疫苗编码=a.疫苗编码))
select min(疫苗剂次) from 临时表
where 疫苗编码=a.疫苗编码))