关于取最大值的问题

lxysjl 2004-09-29 10:06:51
假如我一条记录里面有五个字段.k1,k2,k3,k4,k5 值分别为(2,4,12,1,34)

能像其它的语言一样取出最大值max(k1,k2,k3,k4,k5)得出结果为34.有这样的函数吗.是一个投票系统
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jadesun 2004-09-29
  • 打赏
  • 举报
回复
风云同志的最好,不过我也想练练手,写一个不同的

select kk from (select max(k1) as kk from table
union all
select max(k2) as kk from table
union all
select max(k3) as kk from table
union all
select max(k4) as kk from table
union all
select max(k5) as kk from table) newtable order by kk desc
lxysjl 2004-09-29
  • 打赏
  • 举报
回复
看来没有其它的办法呢.惨
pbsql 2004-09-29
  • 打赏
  • 举报
回复
case when k1>=k2 and k1>=k3 and k1>=k4 and k1>=k5 then k1
when k2>=k3 and k2>=k4 and k2>=k5 then k2
when k3>=k4 and k3>=k5 then k3
when k4>=k5 then k4
else k5
end
pbsql 2004-09-29
  • 打赏
  • 举报
回复
case when k1>k2 then k1 else k2 end

多重组合就是了
lsxaa 2004-09-29
  • 打赏
  • 举报
回复
好像没有这样的函数,可以这样
select max(aa.kk) 最大值
from (select k1 as kk from 表 where 条件 union all
select k2 as kk from 表 where 条件 union all
select k3 as kk from 表 where 条件 union all
select k4 as kk from 表 where 条件 union all
select k5 as kk from 表 where 条件 ) aa



34,587

社区成员

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

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