2015-12-02 97 views
3

我似乎被卡住了,無法找到解決方案。SQL - 選擇連續的最大值

我有一個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 

不過,我覺得這是相當緩慢尤其是我的表比我上面顯示的片段更大量的列。

任何想法?

在此先感謝。

+0

UNION ALL可能更快,但可能不是最好的解決方案。 – jarlh

+1

哪個數據庫? – Bohemian

+0

如果你真的有20列,那麼你可以搜索內置函數。那失敗了,也許你可以創建一個自定義函數。 –

回答

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

+0

這對我很好。謝謝。 – Johnathan

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

這肯定會起作用,但是我的表有20多列,所以這個查詢會變得非常漫長和混亂,可能效率不高。 – Johnathan

+0

@Johnathan,有很多專欄我會用另一個解決方案。 – jarlh

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 
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 
1

我認爲當你將unpivot看作是一個選項時,你是處於正確的軌道上的。 Becaue這正是你想要做的 - 你有一個數據透視表,並且你想從它得到未知值。以下是我想出了:

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 (value for valuetype in (Val1 , Val2 , Val3, ... more values here...)) as u 
0

你總是可以複製這個答案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