求一条sql语句

qyuan 2009-02-16 07:49:24
id 主项1 主项2 主项3 主项3 主项4 主项5 主项6 主项7
1 56 78 35 90 67 80 90 67
2 35 67 46 80 36 45 68 80
一共是7个字段
现在要得到这样的结果
id 主项
1 XX
2 XX
主项的值 是 7个项目 去两个最大 一个最小 然后求平均

sql语句怎么写啊?
...全文
68 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
Terry717 2009-02-17
  • 打赏
  • 举报
回复
--创建用户定义函数
create function f_str(@A1 int, @A2 int, @A3 int, @A4 int,@A5 int,@A6 int,@A7 int)
returns int
as
begin
declare @return int
declare @table table(A int)

insert into @table
select @A1
union all select @A2
union all select @A3
union all select @A4
union all select @A5
union all select @A6
union all select @A7

select @return=avg(A) from @table where A
not in(select top 2 A from @table order by A desc)
and A not in(select min(A) from @table)

return @return
end
go

-- 调用函数来查询
select dbo.f_str(p1,p2,p3,p4,p5,p6,p7) from Project

GO

--创建用户定义函数
drop function f_str

GO
qyuan 2009-02-17
  • 打赏
  • 举报
回复
当 有两个相等的最大值的时候 这函数就是不正确的了.....

34,873

社区成员

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

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