谁来帮大漠写一条Sql语句?

BbbigDesert 2003-10-17 07:10:34
Table1:
FinalNum update1 update2 update3
-------- ------- ------- -------
5 7 8
4 2 6
9 6 3
... ... ...

要求:
用Sql语句实现将每条记录的update1,update2,update3进行比较取出最大值付给FinalNum。
得出结果如下:

Table1:
FinalNum update1 update2 update3
-------- ------- ------- -------
8 5 7 8
6 4 2 6
9 9 6 3
... ... ... ...


如果一条语句实现不了,多条也可以。
...全文
40 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsm0959 2003-10-20
  • 打赏
  • 举报
回复
你能结那分不都归你了?
snowerhuayun 2003-10-20
  • 打赏
  • 举报
回复
我怎么结不了贴啊?
snowerhuayun 2003-10-20
  • 打赏
  • 举报
回复
为高手鼓掌!!
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
或直接
update table1 set FinalNum =
(case when
(case when update1>= update2 then update1 else update2 end)>= update3
then (case when update1>= update2 then update1 else update2 end)
else update3 end)
txlicenhe 2003-10-17
  • 打赏
  • 举报
回复
1:
create function myMax(@a int,@b int)
Returns int
As
begin
declare @c int
if @a >= @b
set @c = @a
else
set @c = @b
Return @c
end

2:
update table1 set FinalNum = dbo.myMax(dbo.myMax(update1,update2),update3)
wea1978 2003-10-17
  • 打赏
  • 举报
回复
@data 定义有误,应为 数值型
wea1978 2003-10-17
  • 打赏
  • 举报
回复
尝试:
'每行进行比较
Dim data As Integer

if select max(update2) as update2 from table1 where 条件 > select max(update1) as update1 from table1 where 条件 then
data=update2
else
data=update1
end if
if select max(update3) as update3 from table1 where 条件 > data then
data= updata3
end if

--将参数传入存储过程

CREATE PROCEDURE updatetable1 --存储过程

@data nvarchar(20)

AS
update table1 set FinalNum=@data where 条件 --每行进行比较

1,216

社区成员

发帖
与我相关
我的任务
社区描述
VB 数据库(包含打印,安装,报表)
社区管理员
  • 数据库(包含打印,安装,报表)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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