日期:2014-05-16 浏览次数:20547 次
select Number,x1,x2,x3,x4,
case when X1 >= x2 and x1 >= x3 and X1>= x4 then x1
when X2 >= x1 and x2 >= x3 and X2>= x4 then X2
when X3 >= x1 and x3 >= x2 and X3>= x4 then X3
else X4
end '最大值'
from cs
select Number,x1,x2,x3,x4,
case when X1 >= x2 and x1 >= x3 and X1>= x4 then x1
when X2 >= x1 and x2 >= x3 and X2>= x4 then X2
when X3 >= x1 and x3 >= x2 and X3>= x4 then X3
else X4
end maxvalue
from cs
--假設x5字段保存最大值
WITH a1 AS
(
SELECT *
FROM cs a
CROSS APPLY
(
SELECT MAX(x1)
FROM
(
SELECT x1 AS maxvalue FROM cs WHERE Number=a.Number
UNION
SELECT x2 FROM cs WHERE Number=a.Number
UNION
SELECT x3 FROM cs WHERE Number=a.Number
UNION
SELECT x4 FROM cs WHERE Number=a.Number
) b
) c
UPDATE a1 SET x5=maxvalue
--5樓少一個括號
--假設x5字段保存最大值
WITH a1 AS
(
SELECT *
FROM cs a
CROSS APPLY
(
SELECT MAX(x1)
FROM
(
SELECT x1 AS maxvalue FROM cs WHERE Number=a.Number
UNION
SELECT x2 FROM cs WHERE Number=a.Number
UNION
SELECT x3 FROM cs WHERE Number=a.Number
UNION
SELECT x4 FROM cs WHERE Number=a.Number
) b
) c
)
UPDATE a1 SET x5=maxvalue