子陌红尘等各位老大,进来看看哈!

samfeng_2003 2005-10-14 04:21:15
今天看到一道题目非常有意思。根据这个改编的。
比如我有一個里一個字段值為﹕1,3,7,7,8,3,3,我想按預定順序(3,7,8,1)排序,結果為﹕3,3,3,7,7,8,1
现在我给出了一个表
col1 col
1 1,3,7,7,3,3,6,6,9,9
2 1,3,5,6,8
3 4,3,3,3,4,5,6
,然后希望能把里面排列成
col1 col
1 3,3,3,7,7,1,6,6,9,9
2 3,8,1,5,6,
3 3,3,3,4,4,5,6
也就是说,按照3,7,8,1的顺序来排序列里面的数字,不在这四个数字里面的,那么就在把前面的排序好以后排在后面就可!
:)
...全文
108 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
samfeng_2003 2005-10-17
  • 打赏
  • 举报
回复
哈哈!谢谢老大们!:) 这两天没有上班,没来看!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
排顺序的时候可以用charindex('','3,4,5,6,7,8,9,1,2,0'),来判断每一个字母的先后

'3,4,5,6,7,8,9,1,2,0'是根据‘3,7,8,1’补出来的!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
4,3,3,23,4,5,6
1,33,2,323,4221,51,622

这些都要考虑!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
如果列的字符串太长,子陌的办法就太慢了!!而且函数中还用了临时表!!

风云兄的题目可以转换成如下:

比如我有一個里一個字段值為﹕1,3,7,7,8,3,3,我想按預定順序(3,7,8,1)排序,結果為﹕3,3,3,7,7,8,1

写一个函数,实现通用的这种转换的功能,函数中部能用临时表!!!

-----
该死的!!明天要加班!! 现在累得很,不想写了。
好羡慕子陌哟!!上班这么轻松!!
子陌红尘 2005-10-14
  • 打赏
  • 举报
回复
create table t(col varchar(2000),col1 int)
insert t select '1,3,7,7,3,3,6,6,9,9',1
insert t select '1,3,5,6,8',2
insert t select '4,3,3,23,4,5,6',3
go


create function f_str(@str varchar(1000))
returns varchar(1000)
as
begin
declare @t table(id int)
while charindex(',',@str)>0
begin
insert into @t select left(@str,charindex(',',@str)-1)
set @str = stuff(@str,1,charindex(',',@str),'')
end
insert into @t select @str

set @str = ''
select
@str = @str+','+rtrim(a.id)
from
(select
top 100 percent id
from
@t
order by
case id
when 3 then 1
when 7 then 2
when 8 then 3
when 1 then 4
else 5
end,id) a
set @str = stuff(@str,1,1,'')
return @str
end
go

select dbo.f_str(col) from t
/*
3,3,3,7,7,1,6,6,9,9
3,8,1,5,6
3,3,4,4,5,6,23
*/

drop function f_str
drop table t
samfeng_2003 2005-10-14
  • 打赏
  • 举报
回复
输入下列数据的时候
col1 col
1 1,3,7,7,3,3,6,6,9,9
2 1,3,5,6,8
3 4,3,3,3,4,5,6

结果是对的
col1 col
1 3,3,3,7,7,1,6,6,9,9
2 3,8,1,5,6,8
3 3,3,3,4,4,5,6

但是如果输入的是
col1 col
1 1,3,7,7,3,3,6,6,9,9
2 1,3,5,6,8
3 4,3,3,23,4,5,6

结果却是,23不能通过。
col1 col
1 3,3,3,7,7,1,6,6,9,9
2 3,8,1,5,6,8
3 3,3,4,24,5,6
samfeng_2003 2005-10-14
  • 打赏
  • 举报
回复
沙兄,我根据你的思路,做了一个出来,但是发现无法解决一个问题!
[create] table t
(col varchar(2000),col1 int)

insert t
select '1,3,7,7,3,3,6,6,9,9',1 [union] all
select '1,3,5,6,8',2 [union] all
select '4,3,3,3,4,5,6',3
go

select col1,col from t

select top 100 identity(int,1,1) as id into # from sysobjects a,sysobjects b

select col1,col into #1 from (
select col1,col=substring(a.col,[id],charindex(',',a.col+',',[id])-b.id)
from t a,# b
where b.[id]<=len(a.col)
and charindex(',',','+a.col,id)=id) a
where col in (3,7,8,1)
order by col1,charindex(col,'3,7,8,1')

declare @col1 varchar(10),@col2 varchar(100)
update #1 set
@col2=case when @col1=col1 then @col2+','+col else col end,
@col1=col1,
col=@col2
select a.col1,a.col+b.col as col from
(select col1,col=max(col) from #1 group by col1) a
inner join
(select col1,col=','+replace(replace(replace(replace(col,'3,',''),'7,',''),'8,',''),'1,','') from t) b
on a.col1=b.col1


drop table #1
drop table #
drop table t
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
写个函数,原理和那个冒泡排序差不多写1!

要是Sqlserver中有数组就好了!!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
或者写一个函数!!字符串改顺序
samfeng_2003 2005-10-14
  • 打赏
  • 举报
回复
呵呵!那里,那里!我看的别人的原贴改编的!我不是能胸口碎大石的哈!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
然后和这个伪表关联

select 1 as order,3 as key unill all
select 2 as order,7 as key unill all
select 3 as order,8 as key unill all
select 4 as order,1 as key unill all

用Order字段来排序,关联用 Key字段

最后还要写一个函数 吧记录还原成字符串!!


---------------

感觉是不是太麻烦了!!

good2speed 2005-10-14
  • 打赏
  • 举报
回复
楼主设计的表真是神奇
samfeng_2003 2005-10-14
  • 打赏
  • 举报
回复
如果列很长,那么循环处理是非常困难的!沙兄!
zxbyhcsdn 2005-10-14
  • 打赏
  • 举报
回复
--//--Split的函数,邹老大写的---

create function f_splitStr
(
@s varchar(8000), --待分拆的字符串
@split varchar(100)--分隔符
)
returns @re table(col varchar(100))
as
begin
-----循环使用Left和Charindex
insert @re values(@s)
return
end

34,590

社区成员

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

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