34,576
社区成员
发帖
与我相关
我的任务
分享
create table [tb](ID int identity(1,1), a int, b int, c int, d int)
insert [tb] select 1,2,3,4 -- max: 4
union all select 5,2,8,3 -- max: 8
union all select 9,7,6,5 -- max: 9
union all select 0,9,2,2 -- max: 9
-- Method 1, by dobear
select ID, Max_abcd=max([value])
from tb
unpivot([value] for [abcd] in ([a], [b], [c], [d])) as U
group by ID
/*
ID Max_abcd
----------- -----------
1 4
2 8
3 9
4 9
(4 行受影响)
*/
--Method 2, by fcuandy
select ID=cast(b.ID as varchar(8)), Max_abcd=cast(b.v as varchar(8))
from (select x=cast((select * from tb for xml path('r')) as xml)) a
cross apply
(select ID=x.query('./ID/text()'),v=x.query('max(./*[local-name(.)!="ID"])')
from a.x.nodes('//r') as t(x)) b
/*
ID Max_abcd
-------- --------
1 4
2 8
3 9
4 9
(4 行受影响)
*/
drop table tb
select id, x.value('max(v)','int') as max_int
from
(select id,
cast(replace('<v>'+ltrim(a)+','+ltrim(b)+
','+ltrim(c)+','+ltrim(d)+'</v>',',','</v><v>') as xml) as x
from [tb]) a
create table [tb](ID int identity(1,1), a int, b int, c int, d int)
insert [tb] select null,null,null,null -- max: null
union all select 1,2,3,4 -- max: 4
union all select 5,2,8,3 -- max: 8
union all select 9,7,6,5 -- max: 9
union all select 0,9,2,2 -- max: 9
select id, x.value('max(v)','int') as max_int
from
(select id,
cast('<v>'+ltrim(a)+'</v><v>'+ltrim(b)+
'</v><v>'+ltrim(c)+'</v><v>'+ltrim(d)+'</v>' as xml) as x
from [tb]) a
/*
id max_int
----------- -----------
1 NULL
2 4
3 8
4 9
5 9
(5 行受影响)
*/
drop table [tb]
create table [tb](ID int identity(1,1), a int, b int, c int, d int)
insert [tb] select 1,2,3,4 -- max: 4
union all select 5,2,8,3 -- max: 8
union all select 9,7,6,5 -- max: 9
union all select 0,9,2,2 -- max: 9
select id, x.query('//v').value('max(v)','int') as max_int
from
(select id,
cast('<root><v>'+ltrim(a)+'</v><v>'+ltrim(b)+
'</v><v>'+ltrim(c)+'</v><v>'+ltrim(d)+'</v></root>' as xml) as x
from [tb]) a
/*
id max_int
----------- -----------
1 4
2 8
3 9
4 9
(4 行受影响)
*/
drop table [tb]
select
ID,
MAX_abcd = CASE WHEN d > (CASE
WHEN c > (CASE WHEN a > b THEN a ELSE b END)
THEN c
ELSE (CASE WHEN a > b THEN a ELSE b END)
END)
THEN d
ELSE (CASE
WHEN c > (CASE WHEN a > b THEN a ELSE b END)
THEN c
ELSE (CASE WHEN a > b THEN a ELSE b END)
END)
END
from tb
/*
ID Max_abcd
-------- --------
1 4
2 8
3 9
4 9
(4 行受影响)
*/
-- 一点建议
from (select x=cast((select * from tb for xml path('r')) as xml)) a
--------------------------------------------------- for xml path('r'), type 这样就是xml 类型了, 不用再转换一次吧?
select
case when a>b....else
from
t
create function fn_getmax(
@id int
)
returns int
as
begin
declare @a int,@b int,@c int,@d int;
select @a=a,@b=b,@c=c,@d=d from tb where @id=id;
if @b>@a
set @a=@b;
if @c>@a
set @a=@c
if @d>@a
set @a=@d
return @a
end
go
select ID,dbo.fn_getmax(ID) as val from tb
;with t(id,val)
as (
select ID,a as val from tb
union all
select ID,b as val from tb
union all
select ID,c as val from tb
union all
select ID,d as val from tb
)
select ID,MAX(val) as val from t group by ID
;with t(id,val)
as (
select ID,a as val from tb
union all
select ID,b as val from tb
union all
select ID,c as val from tb
union all
select ID,d as val from tb
)
select ID,MAX(val) as val from t group by ID
select ID,MAX(a) as val
from (
select ID,a from tb
union all
select ID,b from tb
union all
select ID,c from tb
union all
select ID,d from tb
) as t
group by ID