Sql存储过程代码优化

xkyounet 2012-01-19 06:10:16
请大家看看,我下面这个过程还能优化吗(简洁和效率)?

USE [price]
GO
/****** Object: StoredProcedure [dbo].[getprice] Script Date: 01/19/2012 18:04:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getprice]
@DT nvarchar(20),@PM nvarchar(20),@GG int,@CZ int,@CD int
AS
BEGIN
SET NOCOUNT ON;

declare @pri1 int
declare @pri2 float
declare @pri3 float
declare @pri4 float
declare @price float
set @price=0
select @pri1=AVG(CONVERT(int , JG )) from [price].[dbo].[v_Price] where PM=@PM and GG=@GG and CZ=@CZ and CD=@CD and DT=@DT
select @pri2=AVG(CONVERT(float , JG )) from [price].[dbo].[v_Price] where PM=@PM and GG=@GG and CZ=@CZ and CD=@CD and DATEDIFF(MM, CONVERT(datetime, @DT, 120 ),DT)=1
select @pri3=AVG(CONVERT(float , JG )) from [price].[dbo].[v_Price] where PM=@PM and GG=@GG and CZ=@CZ and CD=@CD and DATEDIFF(MM, CONVERT(datetime, @DT, 120 ),DT)=2
select @pri4=AVG(CONVERT(float , JG )) from [price].[dbo].[v_Price] where PM=@PM and GG=@GG and CZ=@CZ and CD=@CD and DATEDIFF(MM, CONVERT(datetime, @DT, 120 ),DT)=3

if @pri1>=@pri2 set @price=@pri2 else set @price=@pri1
if @pri3<=@price set @price=@pri3
if @pri4<=@price set @price=@pri4

select @price as RE


END
...全文
79 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
faithsong06 2012-01-19
  • 打赏
  • 举报
回复
同求解答。
水族杰纶 2012-01-19
  • 打赏
  • 举报
回复
类似多列取最小值
问题

22,300

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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