SQL 冒泡排序 实现由大到小排序

lzwxyz 2011-05-20 01:50:09
可以实现使用sql脚本写一函数,实现传入6个参数,返回由大到小的字符串吗?

create function dbo.fn_test
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar(50)
as
begin

declare @strtemp nvarchar(50)

//如何实现排序???
return @strtemp

end
go
...全文
801 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lzwxyz 2011-05-20
  • 打赏
  • 举报
回复
果然高手啊!

[Quote=引用 10 楼 acherat 的回复:]
SQL code

create table tb(ic1 int,ic2 int,ic3 int,ic4 int,ic5 int,ic6 int)
insert into tb
select 1,2,3,4,5,6 union all
select 2,12,12,13,24,26 union all
select 1,11,111,23,24,25
go

create fu……
[/Quote]
AcHerat 2011-05-20
  • 打赏
  • 举报
回复

create table tb(ic1 int,ic2 int,ic3 int,ic4 int,ic5 int,ic6 int)
insert into tb
select 1,2,3,4,5,6 union all
select 2,12,12,13,24,26 union all
select 1,11,111,23,24,25
go

create function dbo.fn_test
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar(100)
as
begin
declare @tb table(num int)
declare @te table(id int identity(1,1),num int)
declare @strtemp int

insert into @tb
select @RSCP1 union all select @RSCP2 union all
select @RSCP3 union all select @RSCP4 union all
select @RSCP5 union all select @RSCP6

insert into @te
select num from @tb order by num desc
select @strtemp = (select num from @te where id = 1) - (select num from @te where id = 3)
return @strtemp
end
go

select *
from tb
where dbo.fn_test(ic1,ic2,ic3,ic4,ic5,ic6) >= 5

drop function fn_test
drop table tb


/*

ic1 ic2 ic3 ic4 ic5 ic6
----------- ----------- ----------- ----------- ----------- -----------
2 12 12 13 24 26
1 11 111 23 24 25

(2 行受影响)
Andy-W 2011-05-20
  • 打赏
  • 举报
回复
a b c d e f 是数据库表内的一行数据,需要求出这个6个值的最大于第四最大的差小于5的那行

@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
應該是數值,而不是字符串,不然差值如何計?
lzwxyz 2011-05-20
  • 打赏
  • 举报
回复
这样效率就不是很理想了,17000条记录,要跑很长时间。
有更效率的办法吗?

我想要的效果是:

a b c d e f 是数据库表内的一行数据,需要求出这个6个值的最大于第四最大的差小于5的那行

[Quote=引用 7 楼 acherat 的回复:]
SQL code

create function dbo.fn_test
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar……
[/Quote]
AcHerat 2011-05-20
  • 打赏
  • 举报
回复

create function dbo.fn_test
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar(100)
as
begin
declare @tb table(num int)
declare @strtemp varchar(100)

insert into @tb
select @RSCP1 union all select @RSCP2 union all
select @RSCP3 union all select @RSCP4 union all
select @RSCP5 union all select @RSCP6

select @strtemp = ltrim((select top 1 num from @tb order by num desc) - (select top 1 num from (select top 3 num from @tb order by num desc)t order by num))
return @strtemp
end
go

select dbo.fn_test(1,2,35,6,7,8)

drop function fn_test


/*

----------------------------------------------------------------------------------------------------
28
lzwxyz 2011-05-20
  • 打赏
  • 举报
回复
关键是要求出同一行内的数据
最大值与第四个最大值的差

[Quote=引用 2 楼 ubuntu_fedora 的回复:]
这还要冒泡啊,直接order by完事了
[/Quote]
lzwxyz 2011-05-20
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 ssp2009 的回复:]
SQL code
create function dbo.fn_test
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar(1……
[/Quote]

排序果然可以

传入6个值,我想求出最大值与第四个值的差是否小于5,怎么做呢?
快溜 2011-05-20
  • 打赏
  • 举报
回复
create function dbo.fn_test  
(
@RSCP1 nvarchar(10),
@RSCP2 nvarchar(10),
@RSCP3 nvarchar(10),
@RSCP4 nvarchar(10),
@RSCP5 nvarchar(10),
@RSCP6 nvarchar(10)
)
returns nvarchar(100)
as
begin
declare @tb table(num int)
declare @strtemp varchar(100)

insert into @tb
select @RSCP1 union all select @RSCP2 union all
select @RSCP3 union all select @RSCP4 union all
select @RSCP5 union all select @RSCP6

select @strtemp=isnull(@strtemp+',','')+rtrim(num) from @tb order by num
return @strtemp
end
go

select dbo.fn_test(1,2,35,6,7,8)
/*

------------
1,2,6,7,8,35


jxqn_liu 2011-05-20
  • 打赏
  • 举报
回复
估计是个面试题
Ubuntu_Fedora 2011-05-20
  • 打赏
  • 举报
回复
这还要冒泡啊,直接order by完事了
水族杰纶 2011-05-20
  • 打赏
  • 举报
回复
思路:
将每个字符串插入到表变量里
排序后返回你要字符串

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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