简单存储过程示例优化。。。多列取最小值

来源:互联网 发布:dll不兼容windows 编辑:程序博客网 时间:2024/06/08 12:53
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
原创粉丝点击