Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
S_Lakshmi
Starting Member
22 Posts |
Posted - 2008-10-02 : 11:53:48
|
Dear friends,I wanted to remove the function from the select statement. Here is the script for SP and Function. Could any please help me to solve this issueThanksLakshmi-- Store ProcedureDECLARE @Micrometer_To_Inches AS REALDECLARE @Index AS TINYINT, @Coil_Id as varchar(12)DECLARE @Pass_Num AS TINYINTSET @Index = CHARINDEX('.', @Coil_Id)SET @Micrometer_To_Inches = 25400.0DECLARE @Ret_Value intSET @Ret_Value=nullSELECT Thickness.point_num AS Point_Num, CAST(MAX(Thickness.measurement)/@Micrometer_To_Inches AS DECIMAL(9,5)) AS Max_Gauge,cast( avg((Select top 1 Ret_Value=Porf.measurementFROM Pickle_Profiles AS PorfINNER JOIN Pickle_Length_Info AS Plength ON Plength.msg_num = Porf.msg_num INNER JOIN Pickle_Alias AS PAlias ON PAlias.msg_num = Plength.msg_numWhere Lengthtype='0' andPlength.msg_num =Thickness.msg_num andplenght.point_num = Thickness.point_num)/ @Micrometer_To_Inches AS DECIMAL(9,5)) as avg_gauge,/***********************************************************This is the part i want to change -- Begins***********************************************************/ CAST( AVG(fnPl_Get_Avg_Gauge(Thickness.msg_num, Thickness.point_num, Thickness.measurement))/***********************************************************This is the part i want to change -- Ends***********************************************************// @Micrometer_To_Inches AS DECIMAL(9,5)) AS Avg_Gauge, CAST(STDEV(Thickness.measurement)/@Micrometer_To_Inches AS DECIMAL(9,5)) AS StdDev_GaugeFROM Pickle_Profiles AS Thickness INNER JOIN Pickle_Length_Info AS Length ON Length.msg_num = Thickness.msg_num AND Length.pass_num = @Pass_Num INNER JOIN Pickle_Alias AS Alias ON Alias.msg_num = Length.msg_numWHERE Alias.cid = SUBSTRING(@Coil_Id, 0, @Index) + '.000'GROUP BY Thickness.point_numORDER BY Thickness.point_num/********************************************Function Script (fnPl_Get_Avg_Gauge)*********************************************/( @Msg_Num INT, @Point INT, @Thickness INT)RETURNS INTASBEGIN DECLARE @Msg_Num INT DECLARE @Point INT DECLARE @Thickness INT DECLARE @Ret_Val INT SET @Ret_Val = nullSELECT TOP 1Thickness.measurement FROM IMSWarehouse.dbo.Pickle_Profiles AS Thickness INNER JOIN dbo.Pickle_Length_Info AS Length ON Length.msg_num = Prof.msg_numWHERE Thickness.msg_num = @Msg_Num and Thickness.point_num = @Point and Length.type = '0' RETURN @Ret_ValEND |
|
|
hanbingl
Aged Yak Warrior
652 Posts |
Posted - 2008-10-02 : 12:06:40
|
quote: Originally posted by S_Lakshmi Dear friends,I wanted to remove the function from the select statement. Here is the script for SP and Function. Could any please help me to solve this issueThanksLakshmi-- Store ProcedureDECLARE @Micrometer_To_Inches AS REALDECLARE @Index AS TINYINT, @Coil_Id as varchar(12)DECLARE @Pass_Num AS TINYINTSET @Index = CHARINDEX('.', @Coil_Id)SET @Micrometer_To_Inches = 25400.0DECLARE @Ret_Value intSET @Ret_Value=nullSELECT Thickness.point_num AS Point_Num, CAST(MAX(Thickness.measurement)/@Micrometer_To_Inches AS DECIMAL(9,5)) AS Max_Gauge,cast( avg((Select top 1 Ret_Value=Porf.measurementFROM Pickle_Profiles AS PorfINNER JOIN Pickle_Length_Info AS Plength ON Plength.msg_num = Porf.msg_num INNER JOIN Pickle_Alias AS PAlias ON PAlias.msg_num = Plength.msg_numWhere Lengthtype='0' andPlength.msg_num =Thickness.msg_num andplenght.point_num = Thickness.point_num)/ @Micrometer_To_Inches AS DECIMAL(9,5)) as avg_gauge,/***********************************************************This is the part i want to change -- Begins***********************************************************/ CAST( AVG(fnPl_Get_Avg_Gauge(Thickness.msg_num, Thickness.point_num, Thickness.measurement))/***********************************************************This is the part i want to change -- Ends***********************************************************// @Micrometer_To_Inches AS DECIMAL(9,5)) AS Avg_Gauge,/***********************************************************Try end it here***********************************************************/ CAST(STDEV(Thickness.measurement)/@Micrometer_To_Inches AS DECIMAL(9,5)) AS StdDev_GaugeFROM Pickle_Profiles AS Thickness INNER JOIN Pickle_Length_Info AS Length ON Length.msg_num = Thickness.msg_num AND Length.pass_num = @Pass_Num INNER JOIN Pickle_Alias AS Alias ON Alias.msg_num = Length.msg_numWHERE Alias.cid = SUBSTRING(@Coil_Id, 0, @Index) + '.000'GROUP BY Thickness.point_numORDER BY Thickness.point_num/********************************************Function Script (fnPl_Get_Avg_Gauge)*********************************************/( @Msg_Num INT, @Point INT, @Thickness INT)RETURNS INTASBEGIN DECLARE @Msg_Num INT DECLARE @Point INT DECLARE @Thickness INT DECLARE @Ret_Val INT SET @Ret_Val = nullSELECT TOP 1Thickness.measurement FROM IMSWarehouse.dbo.Pickle_Profiles AS Thickness INNER JOIN dbo.Pickle_Length_Info AS Length ON Length.msg_num = Prof.msg_numWHERE Thickness.msg_num = @Msg_Num and Thickness.point_num = @Point and Length.type = '0' RETURN @Ret_ValEND
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 04:21:27
|
whats the purpose of the function? as of noew it just returns a NULL value for Ret_Val always. do you meant this?( @Msg_Num INT, @Point INT, @Thickness INT)RETURNS INTASBEGIN DECLARE @Msg_Num INT DECLARE @Point INT DECLARE @Thickness INT DECLARE @Ret_Val INT SET @Ret_Val = nullSELECT TOP 1 @Ret_Val =Thickness.measurement FROM IMSWarehouse.dbo.Pickle_Profiles AS Thickness INNER JOIN dbo.Pickle_Length_Info AS Length ON Length.msg_num = Prof.msg_numWHERE Thickness.msg_num = @Msg_Num and Thickness.point_num = @Point and Length.type = '0' RETURN @Ret_ValEND |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-10-03 : 08:37:12
|
| just write the select in the function as an inline view and outer join to it on msg_num and point_num.To get the TOP 1a) Make sure you really need it and your data won't give you dupes (i.e. unique constraint on msg_num & point_num)b) If they do give dupes then you have to use min/max or row_num() partition by ....What you have does not give you deterministic results. Bloody TOP .... It's a stupid function and should not be allowed without order by. |
 |
|
|
|
|
|
|
|