簡體   English   中英

T-SQL 排序整數值

[英]T-SQL sort integer values

我想創建一個過程,稍后將有 5 個整數參數作為輸入。 在這些過程中,我想按升序對這些整數值進行排序,然后更新表。 我用 case whens 這樣做,這樣我就不會丟失每個參數的信息(背景:除此之外,每個參數都連接到一個字符串,我需要按相同的順序排序。但首先對這些整數進行排序更快。 )

不知何故,我無法對中間部分進行排序。 最小值和最大值工作正常:

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;

有人可以提示中間部分嗎?

我不確切知道您想如何使用已排序的整數,但為什么不讓 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM