请问一个SQL语句,谢谢

lj1006 2008-03-22 09:58:58
表TABLE(name)有数据:{a,a,a,b,c,c,b,a}
我想按顺序查出:a,b,c,b,a 即相同的连续的一样的数据查出一个

请问这个SQL语句怎么写啊。谢谢
...全文
124 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2008-03-23
  • 打赏
  • 举报
回复
学习
lijianger 2008-03-23
  • 打赏
  • 举报
回复
8楼的比较高明,学习呀!
肥胖的柠檬 2008-03-22
  • 打赏
  • 举报
回复
select id=identity(int,1,1),* into #a from # --少了这个
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复

drop table t1

create table t1 (mytext varchar(10))
insert into t1
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'b' union all
select 'a'

drop table #

select id=identity(int,1,1),* into # from t1

select mytext
from # a
where not exists(select * from # where mytext=a.mytext and id=a.id+1)
肥胖的柠檬 2008-03-22
  • 打赏
  • 举报
回复
if object_id('tempdb.dbo.#') is not null drop table #
create table # (name varchar(10))
insert into #
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'b' union all
select 'a'


select b.name from #a a FULL JOIN #a b on a.id=b.id+1 where isnull(a.name,'$')<>isnull(b.name,'$')
bulesky_xshp 2008-03-22
  • 打赏
  • 举报
回复
--根据名称产生的时间顺序,产生一个自动增一的编号
Select 'a' as Fname,1 as Fid into Temp_One
insert into Temp_One
Select 'a',2
union all Select 'a',3
union all Select 'b',4
union all Select 'c',5
union all Select 'c',6
union all Select 'b',7
union all Select 'a',8

Select Fname,FID from Temp_One A where not exists (Select Fname from Temp_One where FID=A.FID+1 and Fname = A.Fname) order by FID
drop table Temp_One
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复
想了想,好象用游标行,不用游标不好办。
Limpire 2008-03-22
  • 打赏
  • 举报
回复
--> 测试数据: #
if object_id('tempdb.dbo.#') is not null drop table #
create table # (name varchar(10))
insert into #
select 'a' union all
select 'a' union all
select 'a' union all
select 'b' union all
select 'c' union all
select 'c' union all
select 'b' union all
select 'a'

if object_id('tempdb.dbo.#T') is not null drop table #T
select id=identity(int,1,1),*,cn=cast(null as int) into #T from #

declare @name varchar(10),@i int
select @i = 0

update #T set
cn = case when name = @name then @i else @i+1 end,
@i = case when name = @name then @i else @i+1 end,
@name = name

select name from #T a where id = (select max(id) from #T where cn=a.cn)

/*
name
----------
a
b
c
b
a
*/
lj1006 2008-03-22
  • 打赏
  • 举报
回复
呵呵,请大虾赐教啊!
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复
是这样呀,呵呵。
lj1006 2008-03-22
  • 打赏
  • 举报
回复
我说的是若有数据A,A,A,B,B,A,A 查询结果是A,B,A

用DISTINCT查询结果是A,B,不是我要的
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lj1006 的回复:]
楼上的没看清楚问题把
[/Quote]

我的回复不对吗?
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复
写个示例:

drop table t1

create table t1(
mytext varchar(5)
)

insert into t1
select 'A'
union all select 'A'
union all select 'A'
union all select 'C'
union all select 'C'
union all select 'C'
union all select 'B'
union all select 'B'
union all select 'D'

select *
from t1

select distinct mytext
from t1

--结果:
/*
mytext
------
A
A
A
C
C
C
B
B
D

(所影响的行数为 9 行)

mytext
------
A
B
C
D

(所影响的行数为 4 行)
*/
lj1006 2008-03-22
  • 打赏
  • 举报
回复
楼上的没看清楚问题把
hui_hui_2007 2008-03-22
  • 打赏
  • 举报
回复
select distinct 字段名
from 表名
谢谢大家的支持,我会陆续上传相关电子书 由于体积较大,本书分两卷压缩,请都下载完再解压! Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一) http://download.csdn.net/source/3268267 Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二) http://download.csdn.net/source/3268312 内容简介   本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法,而且可以掌握sql语句和pl/sql的各种基础知识和高级特征(记录类型、集合类型、对象类型、大对象类型)。   除了为读者提供编写sql语句和开发pl/sql块的方法外,本书还为应用开发人员提供了一些常用的pl/sql系统包。通过使用这些pl/sql系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合sql和pl/sql初学者,也适合于有经验的oracle应用开发人员。 前言 第一部分 sql和pl/sql相关工具  第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 操纵数据  第9章 复杂查询  第10章 管理常用对象 第三部分 pl/sql  第11章 pl/sql基础  第12章 访问oracle  第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象类型 第四部分 pl/sql系统包  第23章 使用大对象  第24章 读写os文件  第25章 开发多媒体应用  第26章 开发web应用  第27章 dbms_sq动态sql  第28章 管理统计  第29章 使用数据库资源管理器  第30章 数据加密和解密  第31章 使用调度程序  第32章 使用flashback  第33章 使用重定义联机表  第34章 修正损坏块  第35章 使用日里民挖掘  第36章 使用管道  第37章 使用精细访问控制  第38章 使用精细审计  第39章 使用预警事件  第40章 转换rowid  第41章 其他常用包 习题答案
谢谢大家的支持,我会陆续上传相关电子书 由于体积较大,本书分两卷压缩,请都下载完再解压! Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(一) http://download.csdn.net/source/3268267 Oracle 11g SQL和PL SQL从入门到精通 pdf格式电子书 下载(二) http://download.csdn.net/source/3268312 内容简介   本书是专门为oracle应用开发人员提供的sql和pl/sql编程指南。通过学习本书,读者不仅可以掌握oracle常用工具oracle universal installer、net comfiguration assistant、sql developer、sql*plus的作用及使用方法,而且可以掌握sql语句和pl/sql的各种基础知识和高级特征(记录类型、集合类型、对象类型、大对象类型)。   除了为读者提供编写sql语句和开发pl/sql块的方法外,本书还为应用开发人员提供了一些常用的pl/sql系统包。通过使用这些pl/sql系统包,应用开发人员可以开发出功能更强大的数据库应用程序。本书不仅适合sql和pl/sql初学者,也适合于有经验的oracle应用开发人员。 前言 第一部分 sql和pl/sql相关工具  第1章 在windows 平台上安装oracle database 11g  第2章 配置网络服务名  第3章 使用sql database  第4章 使用sql*plus 第二部分 sql  第5章 sql和pl/sql综述  第6章 简单查询  第7章 sql单行函数  第8章 操纵数据  第9章 复杂查询  第10章 管理常用对象 第三部分 pl/sql  第11章 pl/sql基础  第12章 访问oracle  第13章 编写控制结构  第14章 使用复合数据类型  第15章 使用游标  第16章 异常处理 . 第17章 本地动态sql  第18章 pl/sql过程  第19章 pl/sql函数  第20章 pl/sql包  第21章 触发器  第22章 使用对象类型 第四部分 pl/sql系统包  第23章 使用大对象  第24章 读写os文件  第25章 开发多媒体应用  第26章 开发web应用  第27章 dbms_sq动态sql  第28章 管理统计  第29章 使用数据库资源管理器  第30章 数据加密和解密  第31章 使用调度程序  第32章 使用flashback  第33章 使用重定义联机表  第34章 修正损坏块  第35章 使用日里民挖掘  第36章 使用管道  第37章 使用精细访问控制  第38章 使用精细审计  第39章 使用预警事件  第40章 转换rowid  第41章 其他常用包 习题答案

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧