SQL2005中按列聚合(取N列中的最大最小等)

dobear_0922 2008-11-12 11:12:18
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
...全文
400 47 打赏 收藏 转发到动态 举报
写回复
用AI写文章
47 条回复
切换为时间正序
请发表友善的回复…
发表回复
shanzhongchao 2008-12-04
  • 打赏
  • 举报
回复
谢谢~
huanyuan12 2008-11-28
  • 打赏
  • 举报
回复
顶下,学习中
Zoezs 2008-11-26
  • 打赏
  • 举报
回复
jf
-狙击手- 2008-11-26
  • 打赏
  • 举报
回复
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
-狙击手- 2008-11-26
  • 打赏
  • 举报
回复
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]
wanshichen 2008-11-26
  • 打赏
  • 举报
回复
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

--这样也给点分吧。。。
-狙击手- 2008-11-26
  • 打赏
  • 举报
回复
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]
szzwl 2008-11-13
  • 打赏
  • 举报
回复
接分
JavCof 2008-11-12
  • 打赏
  • 举报
回复

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 行受影响)
*/



CASE WHEN 的嵌套,也算一种方法吧?虽然方法笨了一点,但是积极性很高哈。自己先鼓励下多。
fcuandy 2008-11-12
  • 打赏
  • 举报
回复
否则返回的是xml结构,但不是当作xml类型...
fcuandy 2008-11-12
  • 打赏
  • 举报
回复
[Quote=引用 25 楼 zjcxc 的回复:]
SQL code--一点建议from(selectx=cast((select*fromtbforxml path('r'))asxml)) a--------------------------------------------------- for xml path('r'), type 这样就是xml 类型了, 不用再转换一次吧?
[/Quote]

这样不行的,或者写成 for xml path('r'),type

或者要显式转换, 即用cast 或convert

这样才能调用x方法
zjcxc 元老 2008-11-12
  • 打赏
  • 举报
回复
-- 一点建议
from (select x=cast((select * from tb for xml path('r')) as xml)) a
--------------------------------------------------- for xml path('r'), type 这样就是xml 类型了, 不用再转换一次吧?
viva369 2008-11-12
  • 打赏
  • 举报
回复

select
case when a>b....else
from
t
viva369 2008-11-12
  • 打赏
  • 举报
回复
路过看看!
昵称被占用了 2008-11-12
  • 打赏
  • 举报
回复
函数算不算?

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
昵称被占用了 2008-11-12
  • 打赏
  • 举报
回复
这个2005的,思路简单

;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
昵称被占用了 2008-11-12
  • 打赏
  • 举报
回复
这个2005的,思路简单

;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
昵称被占用了 2008-11-12
  • 打赏
  • 举报
回复
最简单的,2000方法

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
jiang5311 2008-11-12
  • 打赏
  • 举报
回复
帮忙顶一下,给点分吧,呵呵
Garnett_KG 2008-11-12
  • 打赏
  • 举报
回复
.
加载更多回复(27)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧