表格中列的最大值和最小值

时间:2017-09-25 00:09:50

标签: sql-server tsql sql-server-2012 ssms

enter image description here我有一个包含以下数据的表,我正在尝试从表中获取最大和最小列。如果它是一列我可以使用max / min作为列,但在这里我将比较表中的所有列。我需要的是添加新列来存储最大和最小数据。

并且表格中只有一行

create  table 
#test ( column1 int, column2 int , column3 int , column4 int ,column5 int,column6 int)

insert into #test
values( 89, 103,87,67,86,56)

select * from #test 

--drop table #test

提前感谢。

3 个答案:

答案 0 :(得分:2)

最简单的方法是交叉应用列的最小/最大值。例如:

SELECT * 
FROM #test t 
CROSS APPLY (
    SELECT MAX(val), MIN(val) 
    FROM (VALUES (t.column1), (t.column2), (t.column3), (t.column4), (t.column5), (t.column6)) AS c(val)
) AS c(maxvalue, minvalue);

答案 1 :(得分:0)

使用VALUES方法对数据进行取消,然后对其进行重新加固将使语法更加清晰,但确实会带来性能成本(并不是很糟糕,但它确实存在)。使用CASE表达式有点麻烦,但在语法部门,特别是如果要评估很多列,但不会产生相同的性能损失......

SELECT
    t.column1, t.column2, t.column3, t.column4, t.column5, t.column6,
    MinValue = CASE 
                    WHEN t.column1 <= t.column2 AND t.column1 <= t.column3 AND t.column1 <= t.column4 AND t.column1 <= t.column5 AND t.column1 <= t.column6 THEN t.column1
                    WHEN t.column2 <= t.column1 AND t.column2 <= t.column3 AND t.column2 <= t.column4 AND t.column2 <= t.column5 AND t.column2 <= t.column6 THEN t.column2
                    WHEN t.column3 <= t.column1 AND t.column3 <= t.column2 AND t.column3 <= t.column4 AND t.column3 <= t.column5 AND t.column3 <= t.column6 THEN t.column3
                    WHEN t.column4 <= t.column1 AND t.column4 <= t.column2 AND t.column4 <= t.column3 AND t.column4 <= t.column5 AND t.column4 <= t.column6 THEN t.column4
                    WHEN t.column5 <= t.column1 AND t.column5 <= t.column2 AND t.column5 <= t.column3 AND t.column5 <= t.column4 AND t.column5 <= t.column6 THEN t.column5
                    ELSE t.column6
                END,
    MaxValue = CASE 
                    WHEN t.column1 >= t.column2 AND t.column1 >= t.column3 AND t.column1 >= t.column4 AND t.column1 >= t.column5 AND t.column1 >= t.column6 THEN t.column1
                    WHEN t.column2 >= t.column1 AND t.column2 >= t.column3 AND t.column2 >= t.column4 AND t.column2 >= t.column5 AND t.column2 >= t.column6 THEN t.column2
                    WHEN t.column3 >= t.column1 AND t.column3 >= t.column2 AND t.column3 >= t.column4 AND t.column3 >= t.column5 AND t.column3 >= t.column6 THEN t.column3
                    WHEN t.column4 >= t.column1 AND t.column4 >= t.column2 AND t.column4 >= t.column3 AND t.column4 >= t.column5 AND t.column4 >= t.column6 THEN t.column4
                    WHEN t.column5 >= t.column1 AND t.column5 >= t.column2 AND t.column5 >= t.column3 AND t.column5 >= t.column4 AND t.column5 >= t.column6 THEN t.column5
                    ELSE t.column6
                END
FROM
    #test t;

结果...

column1     column2     column3     column4     column5     column6     MinValue    MaxValue
----------- ----------- ----------- ----------- ----------- ----------- ----------- -----------
89          103         87          67          86          56          56          103

答案 2 :(得分:0)

我找到了这个解决方案here

SELECT
    column1
    ,column2
    ,column3
    ,column4
    ,column5
    ,column6
   ,(SELECT MAX(MaxValue)
      FROM (VALUES (column1),(column2),(column3), (column4), (column5), (column6)) AS [Values](MaxValue)) 
   AS MaxValue
   ,(SELECT MIN(MinValue)
      FROM (VALUES (column1),(column2),(column3), (column4), (column5), (column6)) AS [Values](MinValue)) 
   AS MinValue
FROM #test