表A记录:
序号 方向
1 进
2 进
3 进
4 出
5 出
6 出
7 进
8 进
10 进
......想实现这样的结果
1 - 3 进
4 - 6 出
7 - 10 进
请教..........
序号 方向
1 进
2 进
3 进
4 出
5 出
6 出
7 进
8 进
10 进
......想实现这样的结果
1 - 3 进
4 - 6 出
7 - 10 进
请教..........
解决方案 »
- 向各位高手請教了,sql2008打不開
- 2005中命令窗口的工具栏中找不到撤消按钮
- 简单SQL问题
- 谁有<关系型数据库>基础教程!可以共享给我嘛,急!!!谢谢了!
- 有时间帮我瞧一下,急呀
- 请问,在sql中能否随机抽取一个表中的某一行的几个列?
- SQL2005安装不了,求大神,求破
- 用VS 2005 Reporting Services 设计的报表,现在无法发布到SQL Server 2000上。有没有可以解决的方法?
- WHERE后面的字段可以用变量吗?
- 我只有12分了,又有一个问题把我难倒了,我在进行VFP-》SQL SERVER进行数数据转换时出现这个错误,“数据抽取任务要求指定转换”
- 如何将SQL字段中的中文内容选出来放到一个新字段当中?
- 请教 sql server 7 数据库日志怎么清空(郁闷啊!现在还用7.0)
2 开门
3 关门
4 开门
5 开门
6 关门请教是否能查询出改变量,即只过滤出改变过值的相邻记录。结果应如下:
1 开门
3 关门
4 开门
6 关门-- 测试数据
DECLARE @t TABLE(
id int, value varchar(10))
INSERT @t SELECT 1, N'开门'
UNION ALL SELECT 2, N'开门'
UNION ALL SELECT 3, N'关门'
UNION ALL SELECT 4, N'开门'
UNION ALL SELECT 5, N'开门'
UNION ALL SELECT 6, N'关门'-- 查询
SELECT *
FROM @t A
WHERE value <> ISNULL((
SELECT TOP 1 value FROM @t
WHERE id < A.id
ORDER BY id DESC), A.value + 'a')-- 结果:
id value
----------- ----------
1 开门
3 关门
4 开门
6 关门(4 行受影响)
insert @ta
select 1, '进'union all
select 2, '进'union all
select 3, '进'union all
select 4, '出'union all
select 5, '出'union all
select 6, '出'union all
select 7, '进'union all
select 8, '进'union all
select 10, '进'select * from @ta a
where not exists
(select 1 from @ta where name=a.name and id<a.id and id=a.id-1)
and exists
(select 1 from @ta where name=a.name and id>a.id and id=a.id+1) (所影响的行数为 9 行)id name
----------- ----
1 进
4 出
7 进(所影响的行数为 3 行)
insert ta
select 1, '进'union all
select 2, '进'union all
select 3, '进'union all
select 4, '出'union all
select 5, '出'union all
select 6, '出'union all
select 7, '进'union all
select 8, '进'union all
select 10, '进'
select *,
记录=(select count(*)from ta a
where not exists
(select 1 from ta where name=a.name and id<a.id and id=a.id-1) and id!>b.id),
max_id=(select max(id)from ta)
into #--这里生成临时表统计/楼主可以生成视图(简化语句)
from ta b
where not exists
(select 1 from ta where name=b.name and id<b.id and id=b.id-1)
and exists
(select 1 from ta where name=b.name and id>b.id and id=b.id+1)select 显示效果=case when exists(select 1 from # where 记录=a.记录+1 )
then cast(id as varchar)+' — '+(select cast(id-1 as varchar) from # where 记录=a.记录+1 )
else cast(id as varchar)+' — '+cast(max_id as varchar) end,
name
from # a显示效果 name
---------------------------------------------------------------- ----
1 — 3 进
4 — 6 出
7 — 10 进(所影响的行数为 3 行)
insert into @t select 1 ,'进'
insert into @t select 2 ,'进'
insert into @t select 3 ,'进'
insert into @t select 4 ,'出'
insert into @t select 5 ,'出'
insert into @t select 6 ,'出'
insert into @t select 7 ,'进'
insert into @t select 8 ,'进'
insert into @t select 10,'进'select
rtrim(a.序号)+'--'+rtrim(min(b.序号)) as 范围,a.方向
from
(select m.* from @t m where not exists(select 1 from @t where 序号=(select max(序号) from @t where 序号<m.序号) and 方向=m.方向)) a,
(select n.* from @t n where not exists(select 1 from @t where 序号=(select min(序号) from @t where 序号>n.序号) and 方向=n.方向)) b
where
a.方向=b.方向 and a.序号<=b.序号
group by
a.方向,a.序号/*
范围 方向
-------------------------- ----
1--3 进
4--6 出
7--10 进
*/