这种咋弄的,以前看到过,忘了!!!!

SQL77 2009-08-03 08:02:55
...全文
200 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
百年树人 2009-08-06
  • 打赏
  • 举报
回复
下面提供一种思路给你,可能会比他们给你的答案还复杂,但是排序无非也就是这样用
这个过程就是我一般解这种类似题的思路,仅供参考

if object_id('[tb]') is not null drop table [tb] 
go
create table [tb]([value1] int,[value2] int)
insert [tb]
select 1,12 union all
select 1,13 union all
select 1,23 union all
select 0,14 union all
select 0,15 union all
select 1,16 union all
select 0,23 union all
select 0,22 union all
select 1,21 union all
select 1,12


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

select * into #1 from # t where not exists(select 1 from # where value1=t.value1 and id=t.id-1)
select * into #2 from # t where not exists(select 1 from # where value1=t.value1 and id=t.id+1)

select
a.*,
b.px
into #3
from
# a,
(select
a.id as id1,
b.id as id2,
a.px
from
(select *,px=(select count(1)+1 from #1 where id<t.id) from #1 t) a,
(select *,px=(select count(1)+1 from #2 where id<t.id) from #2 t) b
where
a.px=b.px) b
where a.id between b.id1 and b.id2

select
value1,
value2,
col=(select count(1)+1 from #3 where px=t.px and id<t.id)
from
#3 t

drop table #,#1,#2,#3

/**
value1 value2 col
----------- ----------- -----------
1 12 1
1 13 2
1 23 3
0 14 1
0 15 2
1 16 1
0 23 1
0 22 2
1 21 1
1 12 2

(所影响的行数为 10 行)
**/
百年树人 2009-08-06
  • 打赏
  • 举报
回复
以前的帖子应该跟这个是不一样的
如果是:

value1 value2
----------- -----------
1 12
1 13
1 23
2 14
2 15
3 16
4 23
4 22
5 21
5 12

这样的格式就很容易做到
SQL77 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 sql77 的回复:]
引用 16 楼 josy 的回复:
原理是相通的,今天有点事,明天再帮你写代码

3Q树哥了
[/Quote]
树哥,麻烦了,我找了以前的帖子,没看到,不知道在哪了
SQL77 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 josy 的回复:]
原理是相通的,今天有点事,明天再帮你写代码
[/Quote]
3Q树哥了
SQL77 2009-08-04
  • 打赏
  • 举报
回复
[Quote=引用 18 楼 sql77 的回复:]
引用 17 楼 sql77 的回复:
引用 16 楼 josy 的回复:
原理是相通的,今天有点事,明天再帮你写代码

3Q树哥了

树哥,麻烦了,我找了以前的帖子,没看到,不知道在哪了
[/Quote]
树大哥,有没有时间啊?????等待
百年树人 2009-08-03
  • 打赏
  • 举报
回复
原理是相通的,今天有点事,明天再帮你写代码
SQL77 2009-08-03
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([value1] int,[value2] int)
insert [tb]
select 1,12 union all
select 1,13 union all
select 1,23 union all
select 0,14 union all
select 0,15 union all
select 1,16 union all
select 0,23 union all
select 0,22 union all
select 1,21 union all
select 1,12

树哥上面我已经理解了,
呵呵,我就是想弄成我楼上的那种效果,以前好像看到树哥做个这样的,可忘了是什么时候


就是想给这个表查询时增加一列排序的,结果就是我楼上的那样


value1 value2 COL3
----------- -----------
1 12 1
1 13 2
1 23 3
0 14 1
0 15 2
1 16 1
0 23 1
0 22 2
1 21 1
1 12 2
百年树人 2009-08-03
  • 打赏
  • 举报
回复
把你的语句贴出来
SQL77 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 josy 的回复:]
select value1,value2 from # t where not exists(select * from # where value1=t.value1 and id=t.id-1)


就是找一个序列里最小的
[/Quote]
呵呵,树哥我就是想要那种排序,再取最小的就是结果了,可就是排不出来,
百年树人 2009-08-03
  • 打赏
  • 举报
回复
select value1,value2 from # t where not exists(select * from # where value1=t.value1 and id=t.id-1)


就是找一个序列里最小的
百年树人 2009-08-03
  • 打赏
  • 举报
回复
你可以所有一起来排序

不过你这种效果更好啊,取后面那一列为1的就可以了
SQL77 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 josy 的回复:]
引用 8 楼 sql77 的回复:
引用 7 楼 herowang 的回复:
老树不是已经给解决了吗?

嗯,呵呵,学习树哥,谢谢树哥的,题目我都没看清楚,树哥排序的怎么弄的,



就是不排序,再增加一个行号,2000不能使用行号,所以用临时表,加一个自增列,
然后根据行号或自增列来排序
[/Quote]
树哥,
是这种效果,还望树哥赐教
value1 value2
----------- -----------
1 12 1
1 13 2
1 23 3
0 14 1
0 15 2
1 16 1
0 23 1
0 22 2
1 21 1
1 12 2

(所影响的行数为 10 行)
百年树人 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 sql77 的回复:]
引用 7 楼 herowang 的回复:
老树不是已经给解决了吗?

嗯,呵呵,学习树哥,谢谢树哥的,题目我都没看清楚,树哥排序的怎么弄的,
[/Quote]


就是不排序,再增加一个行号,2000不能使用行号,所以用临时表,加一个自增列,
然后根据行号或自增列来排序
SQL77 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 herowang 的回复:]
老树不是已经给解决了吗?
[/Quote]
嗯,呵呵,学习树哥,谢谢树哥的,题目我都没看清楚,树哥排序的怎么弄的,
  • 打赏
  • 举报
回复
老树不是已经给解决了吗?
xiequan2 2009-08-03
  • 打赏
  • 举报
回复
declare @t table( value1 int,value2 int)

insert into @t
select 1,12 union all
select 1,13 union all
select 1,23 union all
select 0,14 union all
select 0,15 union all
select 1,16 union all
select 0,23 union all
select 0,22 union all
select 1,21 union all
select 1,21

;
with cte1 as
(
select id=row_number() over (order by getdate()),* from @t
)
,
cte2 as
(
SELECT *,flag = (SELECT COUNT(*) FROM cte1
WHERE id < t.id AND value1<>t.value1) from cte1 t
)

select value1,value2 from cte2 c2
where not exists (select 1 from cte2 where value1=c2.value1 and flag=c2.flag and id<c2.id)

/*
value1 value2
----------- -----------
1 12
0 14
1 16
0 23
1 21

(5 行受影响)
*/
百年树人 2009-08-03
  • 打赏
  • 举报
回复
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([value1] int,[value2] int)
insert [tb]
select 1,12 union all
select 1,13 union all
select 1,23 union all
select 0,14 union all
select 0,15 union all
select 1,16 union all
select 0,23 union all
select 0,22 union all
select 1,21 union all
select 1,12

---查询---
select id=identity(int,1,1),* into # from [tb]

select value1,value2 from # t where not exists(select * from # where value1=t.value1 and id=t.id-1)

---结果---
value1 value2
----------- -----------
1 12
0 14
1 16
0 23
1 21

(所影响的行数为 5 行)

sql2005以上的版本可以用行号
guguda2008 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sql77 的回复:]
引用 1 楼 guguda2008 的回复:
SQL codeDECLARE@TTABLE(
COL1INT,
COL2INT
)INSERTINTO@TSELECT0,1UNIONALLSELECT1,3UNIONALLSELECT0,9UNIONALLSELECT1,15UNIONALLSELECT1,18UNIONALLSELECT0,22UNIONALLSELECT0,27UNIONALLSELECT1,6SELECT*FROM@T ¡­

宝鸭不是这样子的啦,
[/Quote]
哪不对了
SQL77 2009-08-03
  • 打赏
  • 举报
回复
呜...大虾来看下呀
SQL77 2009-08-03
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 guguda2008 的回复:]
SQL codeDECLARE@TTABLE(
COL1INT,
COL2INT
)INSERTINTO@TSELECT0,1UNIONALLSELECT1,3UNIONALLSELECT0,9UNIONALLSELECT1,15UNIONALLSELECT1,18UNIONALLSELECT0,22UNIONALLSELECT0,27UNIONALLSELECT1,6SELECT*FROM@T ¡­
[/Quote]
宝鸭不是这样子的啦,
加载更多回复(1)

34,873

社区成员

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

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