SQL - Select 一行中的最大值

SQL - Select the largest value within a row

我似乎被困在这个问题上,环顾四周找不到解决方案。

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

Name   Val1   Val2   Val3
John   1000   2000   3000

我需要做的是 Select 这一行中的最大值,即 3000

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

我也看过 PIVOTUNPIVOT 的用途,但我认为它们在这里对我没有用..

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

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

但是我觉得这相当慢,尤其是因为我的 table 的列比我上面显示的代码段多得多。

有什么想法吗?

提前致谢。

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

你需要这样的东西:

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

您可以通过 APPLY 为列构建引用 table 并使用本机 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

使用数学逻辑:

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

我认为当您将 unpivot 作为一个选项时,您走在了正确的轨道上。因为这正是您想要做的 - 您有一个枢轴 table,并且您想要从中获得非枢轴值。这是我想出的:

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

要扩展到整个 table,您只需将更多列名称添加到逆透视行即可:

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

你总是可以复制这个答案Is there a Max function in SQL Server that takes two values like Math.Max in .NET?

-- 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),
    ('Tony66', 1, 2, 3, 4, 5, 66),
    ('Tony55', 1, 2, 3, 4, 55, 6),
    ('Tony44', 1, 2, 3, 44, 5, 6),
    ('Tony33', 1, 2, 33, 4, 5, 6),
    ('Tony22', 1, 22, 3, 4, 5, 6),
    ('Tony11', 11, 2, 3, 4, 5, 6)

SELECT name,
       (SELECT MAX(value)
        FROM (VALUES (Val1),(Val2), (Val3), (Val4), (Val5), (Val6)) AS AllValues(value)) AS 'MaxValue'
FROM @data