SQL - 选择行中的最大值

时间:2015-12-02 09:14:06

标签: sql max ssms

我似乎陷入了困境,无法找到解决方案。

我有一个SQL表,第一行看起来像这样:

Name   Val1   Val2   Val3
John   1000   2000   3000

我需要做的是选择此行中的最大值,即3000

显然,如果这些值在列而不是行中,您可以使用SELECT MAX(column) FROM table来获取列中的最大值。 是否有相当于这个用于查找连续的最大值?

我还看了PIVOTUNPIVOT的用法,但我不认为它们对我有用..

我能够做到的唯一方法是创建一个临时表并将每个值插入到一个列中,如下所示:

CREATE TABLE #temp (colvals float)
     INSERT INTO #temp (colvals)
          SELECT Val1 FROM table WHERE ID=1
         UNION
          SELECT Val2 FROM table WHERE ID=1
         UNION
          SELECT Val3 FROM table WHERE ID=1
--------------------------------------------
SELECT MAX(colvals) FROM #temp
--------------------------------------------
DROP TABLE #temp

但是我觉得这很慢,特别是因为我的表格比我上面显示的代码段要多得多。

有什么想法吗?

提前致谢。

6 个答案:

答案 0 :(得分:3)

您可以按APPLY为列构建参考表,并使用原生MAX()

-- Sample Data
declare @data table (Name varchar(10), Val1 int, Val2 int, Val3 int, Val4 int, Val5 int, Val6 int)
insert @data values 
    ('John', 1000, 2000, 3000, 4000, 5000, 6000),
    ('Mary', 1, 2, 3, 4, 5, 6)


select Name, MaxValue from 
    @data 
    cross apply 
    (
        select max(value) as MaxValue 
        from 
            (values
                (Val1),(Val2),(Val3),(Val4),(Val5),(Val6) -- Append here
            ) t(value)
    ) result

SQL Fiddle

答案 1 :(得分:1)

select MAX(case when c1 > c2 and c1 > c3 then c1
                when c2 > c3 then c2
                else c3
           end)
from tablename

答案 2 :(得分:1)

你需要这样的东西:

SELECT *, Row_Number() OVER (ORDER BY GETDATE()) Rowid INTO #temp From yourtable

DECLARE @Columns AS Varchar(MAX)
SET @Columns =''
SELECT @Columns = @Columns + ',[' + name + ']' FROM tempdb..syscolumns 
WHERE id=object_id('tempdb..#temp') AND name <> 'Rowid'

SELECT @Columns = Right(@Columns, len(@Columns)-1)
exec ('Select Rowid,Max(val) maxval from #temp t Unpivot(val For data in (' + @Columns + ')) as Upvt Group by Rid')

Drop table #temp

答案 3 :(得分:1)

当你将unpivot视为一种选择时,我认为你走在了正确的轨道上。这就是你想要做的事情 - 你有一个数据透视表,你想从中获得不重要的价值。这就是我想出的:

declare @base table (Name char(4),   Val1   int, Val2   int ,Val3 int);
insert into @base (Name,   Val1 ,  Val2 ,  Val3) values ('John' ,  1000  , 2000 ,  3000);

select name, max(value) as max_value 
from (
    select name, valuetype, value
    from  @base b
    unpivot ( value for valuetype in (Val1 ,  Val2 ,  Val3)) as u 
     ) as up
group by name

要扩展到整个表,您只需向unpivot行添加更多列名称:

unpivot ( value for valuetype in (Val1 ,  Val2 ,  Val3, ... more values here...)) as u

答案 4 :(得分:0)

使用数学逻辑:

select 
  case
    when val1 >= val2 and val1 >= val2 then val1
    when val2 >= val1 and val2 >= val3 then val2
    else val3
  end maxVal
from mytable
where id = 1

答案 5 :(得分:0)

您可以随时复制此答案Is there a Max function in SQL Server that takes two values like Math.Max in .NET?

When you look in the mirror :

    a - You smile = profile handsom -> 3
                  = profile cute -> 2

    b - You run   = profile shy -> 3
                  = profile uglly -> 1

    c - Who's that = profile yolo -> 2
                   = profile zen -> 3