请问一个SQL语句,谢谢

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

请问这个SQL语句怎么写啊。谢谢
...全文
128 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用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 表名

34,838

社区成员

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

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