SQL Server 2012 veya sonraki sürümlerde, en fazla 2 değeri elde etmek için IIFve ISNULL(veya COALESCE) birleşimlerini kullanabilirsiniz .
1 tanesi NULL olsa bile.
IIF(col1 >= col2, col1, ISNULL(col2, col1))
Veya her ikisi de NULL olduğunda 0 döndürmesini istiyorsanız
IIF(col1 >= col2, col1, COALESCE(col2, col1, 0))
Örnek pasaj:
-- use table variable for testing purposes
declare @Order table
(
OrderId int primary key identity(1,1),
NegotiatedPrice decimal(10,2),
SuggestedPrice decimal(10,2)
);
-- Sample data
insert into @Order (NegotiatedPrice, SuggestedPrice) values
(0, 1),
(2, 1),
(3, null),
(null, 4);
-- Query
SELECT
o.OrderId, o.NegotiatedPrice, o.SuggestedPrice,
IIF(o.NegotiatedPrice >= o.SuggestedPrice, o.NegotiatedPrice, ISNULL(o.SuggestedPrice, o.NegotiatedPrice)) AS MaxPrice
FROM @Order o
Sonuç:
OrderId NegotiatedPrice SuggestedPrice MaxPrice
1 0,00 1,00 1,00
2 2,00 1,00 2,00
3 3,00 NULL 3,00
4 NULL 4,00 4,00
Ama eğer birden fazla değeri TOPLAMALARI gerekiyorsa?
Daha sonra DEĞERLERİN bir araya gelmesi için ÇAPRAZ UYGULAMASI öneririm.
Bu aynı zamanda diğer şeyleri aynı anda hesaplayabilmesi avantajına da sahiptir.
Misal:
SELECT t.*
, ca.[Total]
, ca.[Maximum]
, ca.[Minimum]
, ca.[Average]
FROM SomeTable t
CROSS APPLY (
SELECT
SUM(v.col) AS [Total],
MIN(v.col) AS [Minimum],
MAX(v.col) AS [Maximum],
AVG(v.col) AS [Average]
FROM (VALUES (t.Col1), (t.Col2), (t.Col3), (t.Col4)) v(col)
) ca
GREATESTişlev olarak uygulanır; SQLite,MAXtoplamda birden çok sütuna izin vererek desteği taklit eder .