id喜欢创建一个过程,稍后将有5个整数参数作为输入。在这些过程中,我想按升序对这些整数值进行排序,然后更新表。我用case case做了这个,所以我不会丢失每个参数的信息(背景:除此之外,每个参数都连接到我需要的字符串,所以按照相同的顺序排序。但是先排序这些整数更快。)< / p>
不知何故,我无法获得中间部分的排序。最小值和最大值工作正常:
DECLARE @tableX TABLE(c1 INT, c2 INT, c3 INT, c4 INT, c5 INT);
INSERT @tableX(c1,c2,c3,c4,c5)
SELECT 2,1,3,0,1
UNION ALL SELECT 3,4,5,2,2
UNION ALL SELECT 5,4,3,1,1
UNION ALL SELECT 3,1,2,0,10;
SELECT
--int1
c1 = CASE
WHEN c1 >= c2 AND c1 >= c3 AND c1 >= c4 AND c1 >= c5 THEN c1
WHEN c2 >= c1 AND c2 >= c3 AND c2 >= c4 AND c2 >= c5 THEN c2
WHEN c3 >= c1 AND c3 >= c2 AND c3 >= c4 AND c3 >= c5 THEN c3
WHEN c4 >= c1 AND c4 >= c2 AND c4 >= c3 AND c4 >= c5 THEN c4
ELSE c5 END,
--int2
--c2 = CASE
-- WHEN c1 >= c2 AND c1 >= c3 AND c1 >= c4 AND c1 >= c5 THEN
-- CASE WHEN c2 >= c3 AND c2 >= c4 AND c2 >= c5 THEN c2 ELSE
-- CASE WHEN c3 >= c2 AND c3 >= c5 THEN
-- CASE WHEN c4 >= c5 THEN
-- WHEN c2 >= c3 AND c2 >= c4 AND c2 >= c5 THEN
-- CASE WHEN c2 >= c3 AND c2 >= c4 AND c2 >= c5 THEN c2 END
--ELSE
--CASE WHEN c3 >= c2 AND c3 >= c4 AND c3 >= c5 THEN c3 ELSE
--CASE WHEN c4 >= c5 THEN c4 ELSE c5 END END END END,
--int3
c3 = NULL,
--in4
c4 = NULL,
--in5
c5 = CASE
WHEN c1 <= c2 AND c1 <= c3 AND c1 <= c4 AND c1 <= c5 THEN c1
WHEN c2 <= c1 AND c2 <= c3 AND c2 <= c4 AND c2 <= c5 THEN c2
WHEN c3 <= c1 AND c3 <= c2 AND c3 <= c4 AND c3 <= c5 THEN c3
WHEN c4 <= c1 AND c4 <= c2 AND c4 <= c3 AND c4 <= c5 THEN c4
ELSE c5 END
FROM @tableX;
有人可以提示中间部分吗? 提前致谢。
答案 0 :(得分:0)
我不知道你想如何使用排序整数,但为什么不让SQL为你做排序呢?在下面,创建一个临时表(@s
),其中包含五个整数(作为参数传入?)并对它们进行排序。
declare @ip1 int = 2
declare @ip2 int = 1
declare @ip3 int = 3
declare @ip4 int = 0
declare @ip5 int = 1
declare @s table (v int)
INSERT @s(v)
values((@ip1)), ((@ip2)), ((@ip3)), ((@ip4)), ((@ip5))
insert
select v from @s order by v
使用此排序列表在存储过程中执行的操作是另一个问题...
好的,这是一个尝试和完成的脚本。这是T-SQL。请运行它并查看输出。第一部分创建一个虚拟表来表示您的表并用一些数据填充它。它假定您的表具有整数主键。
CREATE TABLE dbo.sortTbl
( id int IDENTITY(1,1) Primary Key NOT NULL,
i1 int NOT NULL, i2 int NOT NULL, i3 int NOT NULL,
i4 int NOT NULL, i5 int NOT NULL,
s1 varchar(20) NULL, s2 varchar(20) NULL, s3 varchar(20) NULL,
s4 varchar(20) NULL, s5 varchar(20) NULL)
insert sortTbl(i1, i2, i3, i4, i5, s1, s2, s3, s4, s5)
Values ((3), (1), (0), (5), (7), ('fog'), ('snap'), ('dead'), ('yellow'), ('lox')),
((6), (2), (12), (1), (8),('tree'), ('saw'), ('earn'), ('ran'), ('que')),
((5), (6), (0), (2), (3),('like'), ('car'), ('hood'), ('wash'), ('man'))
下一部分是从此示例表(`sortTbl')中的数据生成额外十列的脚本。即使原始值不仅是1到5,它也可以工作,但在每一行中不应该有重复。
declare @T table (id int primary key not null,
ni1 int null, ni2 int null, ni3 int null,
ni4 int null, ni5 int null,
ns1 varchar(20) null, ns2 varchar(20) null,
ns3 varchar(20) null, ns4 varchar(20) null,
ns5 varchar(20) null)
insert @t(id) select id from sortTbl
declare @d table (id int, val int, strVal varchar(20))
declare @id int = 0
declare @S table (rn int primary key identity not null,
id int, colOrder int null, val int, strVal varchar(20))
While exists (Select * from @t
Where id > @id) Begin
select @id = Min(id)
from sortTbl where id > @id
insert @d(id, val, strVal)
Select @id, i1, s1 From sortTbl where id = @id union
Select @id, i2, s2 From sortTbl where id = @id union
Select @id, i3, s3 From sortTbl where id = @id union
Select @id, i4, s4 From sortTbl where id = @id union
Select @id, i5, s5 From sortTbl where id = @id
-- -------------------------------------------
Insert @s(id, val, strVal)
Select id, val, strVal from @d
order by val
Delete @d
end
Update s set colOrder =
(Select Count(*) from @s
Where id = s.id
and rn <= s.rn)
From @s s
Select a.Id,
c1.Val, c1.strVal,
c2.Val, c2.strVal,
c3.Val, c3.strVal,
c4.Val, c4.strVal,
c5.Val, c5.strVal
From sortTbl a
left join @s c1 on c1.id = a.id and c1.colOrder = 1
left join @s c2 on c2.id = a.id and c2.colOrder = 2
left join @s c3 on c3.id = a.id and c3.colOrder = 3
left join @s c4 on c4.id = a.id and c4.colOrder = 4
left join @s c5 on c5.id = a.id and c5.colOrder = 5