2016-11-29 117 views
0

我正在尋找一種方法從表中的單一行中選取最高和最低值(整數)。有4列,我需要比較在一起,並獲得最高和最低的數字。SQL:從一行中選取最高和最低值(整數)

表看起來是這樣的......

id | name | col_to_compare1 | col_to_compare2 | col_to_compare3 | col_to_compare4 

1 | John | 5 | 5 | 2 | 1 

2 | Peter | 3 | 2 | 4 | 1 

3 | Josh | 3 | 5 | 1 | 3 

你能幫助我嗎?謝謝!

+0

什麼是所需的輸出? – Backtrack

+0

具有最高價值的另一列和具有最低價值的下一個列。 – dusty

+2

[SQL Server等效於Oracle LEAST?]的可能重複(http://stackoverflow.com/questions/1972051/sql-server-equivalent-to-oracle-least) – JohnHC

回答

0

這些解決方案保留當前行並添加最小/最大值的其他列。

select  * 

from  t cross apply 

      (select min(col) as min_col 
        ,max(col) as max_col 

      from (
         values 

         (t.col_to_compare1) 
         ,(t.col_to_compare2) 
         ,(t.col_to_compare3)  
         ,(t.col_to_compare4) 

        ) c(col) 
      ) c 

OR

select  * 
      ,cast ('' as xml).value ('min ((sql:column("t.col_to_compare1"),sql:column("t.col_to_compare2"),sql:column("t.col_to_compare3"),sql:column("t.col_to_compare4")))','int') as min_col 
      ,cast ('' as xml).value ('max ((sql:column("t.col_to_compare1"),sql:column("t.col_to_compare2"),sql:column("t.col_to_compare3"),sql:column("t.col_to_compare4")))','int') as max_col 

from  t 

+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+ 
| id | name | col_to_compare1 | col_to_compare2 | col_to_compare3 | col_to_compare4 | min_col | max_col | 
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+ 
| 1 | John | 5    | 5    | 2    | 1    | 1  | 5  | 
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+ 
| 2 | Peter | 3    | 2    | 4    | 1    | 1  | 4  | 
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+ 
| 3 | Josh | 3    | 5    | 1    | 3    | 1  | 5  | 
+----+-------+-----------------+-----------------+-----------------+-----------------+---------+---------+ 
+0

這是完美的!非常感謝你! – dusty

3

您可以使用CROSS APPLYVALUES子句執行此操作。使用VALUES將所有比較列分組,然後選擇最大值。

SELECT 
     MAX(d.data1) as MaxOfColumns 
     ,MIN(d.data1) as MinOfColumns 
     ,a.id 
     ,a.name 
    FROM YOURTABLE as a 
    CROSS APPLY ( 
       VALUES(a.col_to_compare1) 
        ,(a.col_to_compare2) 
        ,(a. col_to_compare3) 
        ,(a.col_to_compare4) 
        ,(a. col_to_compare5) 
       ) as d(data1) --Name the Column 
    GROUP BY a.id 
      ,a.name 
0

的一種方式做,這是對 「破發」 除了數據

declare @table table (id int, name varchar(10), col1 int, col2 int, col3 int, col4 int) 

insert into @table values (1 , 'John' , 5 , 5 , 2 , 1) 
insert into @table values (2 , 'Peter' , 3 , 2 , 4 , 1) 
insert into @table values (3 , 'Josh' , 3 , 5 , 1 , 3) 


;with stretch as 
(
    select id, col1 as col from @table 
    union all 
    select id, col2 as col from @table 
    union all 
    select id, col3 as col from @table 
    union all 
    select id, col4 as col from @table 
) 

select 
    t.id, 
    t.name, 
    agg.MinCol, 
    agg.MaxCol 
from @table t 
    inner join 
    (
     select 
      id, min(col) as MinCol, max(col) as MaxCol 
     from stretch 
     group by id 
    ) agg 
     on t.id = agg.id 
1

假設你正在尋找每行最小/最大

Declare @YourTable table (id int,name varchar(50),col_to_compare1 int,col_to_compare2 int,col_to_compare3 int,col_to_compare4 int) 
Insert Into @YourTable values 
(1,'John',5,5,2,1), 
(2,'Peter',3,2,4,1), 
(3,'Josh',3,5,1,3) 

Select A.ID 
     ,A.Name 
     ,MinVal = min(B.N) 
     ,MaxVal = max(B.N) 
From @YourTable A 
Cross Apply (Select N From (values(a.col_to_compare1),(a.col_to_compare2),(a.col_to_compare3),(a.col_to_compare4)) N(N)) B 
Group By A.ID,A.Name 

返回

ID Name MinVal MaxVal 
1 John 1  5 
3 Josh 1  5 
2 Peter 1  4 
0

似乎很簡單

SELECT min(col1), max(col1), min(col2), max(col2), min(col3), max(col3), min(col4), max(col4) FROM table 

帶給你的最小值和最大值爲每個山坳UMN。

繼OP的評論後,我相信他可能會尋找被查詢者分組的最小/最大值。

因此,這將是:

SELECT name, min(col1), max(col1), min(col2), max(col2), min(col3), max(col3), min(col4), max(col4) FROM table GROUP BY name 
+0

是的,列,但我需要從行中的值。 Dudu上面的解決方案是完美的。 – dusty

相關問題