SELECT ( SELECT MAX(col) FROM( SELECT col = r0 UNION ALL SELECT col = r1 UNION ALL SELECT col = r2 )A ) FROM 表a
可以写个函数来返回最大值啊...create function compare(@r0 int,@r1 int,@r2 int) returns int as begin declare @tmp int set @tmp = @r0 if(@tmp < @r1) set @tmp = @r1 if(@tmp < @r2) set @tmp = @r2 return @tmp endselect compare(r0,r1,r2) from 表a
if not object_id('dbo.Funtion_Test')is null drop function Funtion_Test go create function Funtion_Test(@r0 int,@r1 int,@r2 int) returns int as begin declare @t table(R int) declare @max int insert @t select @r0 insert @t select @r1 insert @t select @r2 select @max=max(R) from @t return @max end go declare @t table(r0 int, r1 int , r2 int) insert @t select 20 , -30, 50 select dbo.Funtion_Test(r0,r1,r2) from @t ----------- 50(1 行受影响)
(
SELECT
MAX(col)
FROM(
SELECT col = r0 UNION ALL
SELECT col = r1 UNION ALL
SELECT col = r2
)A
)
FROM 表a
returns int
as
begin
declare @tmp int
set @tmp = @r0
if(@tmp < @r1)
set @tmp = @r1
if(@tmp < @r2)
set @tmp = @r2 return @tmp
endselect compare(r0,r1,r2) from 表a
drop function Funtion_Test
go
create function Funtion_Test(@r0 int,@r1 int,@r2 int)
returns int
as
begin
declare @t table(R int)
declare @max int
insert @t select @r0
insert @t select @r1
insert @t select @r2
select @max=max(R) from @t
return @max
end
go
declare @t table(r0 int, r1 int , r2 int)
insert @t select 20 , -30, 50
select dbo.Funtion_Test(r0,r1,r2) from @t -----------
50(1 行受影响)
沒有時用1樓方法;05可用CTE處理
我试了试,运行报错啊,这里面的“A”是什么意思?希望大虾给解释下
r0 r1 r2 r3
张三 250 男 38只想取出r1 和 r3 的最大值,即只取数字的!!