| Author |
Topic |
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 10:39:16
|
| Hi,i need for my project to retrive the AVG of a single row.how it can be done? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 10:44:53
|
| SELECT (Col1 + Col2 + Col3) / 3.0FROM Table1Peter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 10:52:41
|
| is there a way to create it dynamically without knowing the amoung of columns in the table? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-17 : 10:53:43
|
Yes. There is. But why would you don't know the number of columns in the table ?Smells like a case of bad design. KH |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 11:16:36
|
on the contrary my friend,i know the number of column but i want to make the function usable for all kind of tables.anyhow, here is the code and error i get:set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOalter FUNCTION [dbo].[GetPlayerAvg]( @playerid int,@calcType varchar(20) )RETURNS floatASBEGINdeclare @overAll as float,declare @physic as float,declare @technic as floatif @calcType='overall'beginset @physic=SELECT ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13FROM PhisicalPropertiesTablewhere playerid=@playeridset @technic=SELECT (Goalkeeper+ Corners+Crossing+Dribbling+Finishing+FirstTouch+Heading+LongShots+LongThrows+Marking+Passing+Penalty+Tackling+ThrowIns+ Aerial+AreaCommand+Communication+Handling+Kicking+OneOnOne+Reflex+Throwing+Positioning+OffBall+Creativity+Leadership+ShootingPower+ LongPass+Crosses)/29FROM TechnicalPropertiesTablewhere playerid=@playerid@overAll=(@technic+@physic)/2return (@overAll)endelse if @calcType='physic'beginset @physic=SELECT ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13FROM PhisicalPropertiesTablewhere playerid=@playeridreturn(@physic)endelse if @calcType='technic'beginset @technic=SELECT (Goalkeeper+ Corners+Crossing+Dribbling+Finishing+FirstTouch+Heading+LongShots+LongThrows+Marking+Passing+Penalty+Tackling+ThrowIns+ Aerial+AreaCommand+Communication+Handling+Kicking+OneOnOne+Reflex+Throwing+Positioning+OffBall+Creativity+Leadership+ShootingPower+ LongPass+Crosses)/29FROM TechnicalPropertiesTablewhere playerid=@playeridreturn (@technic)endEND quote: Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 7Incorrect syntax near the keyword 'declare'.Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 8Incorrect syntax near the keyword 'declare'.Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 13Incorrect syntax near the keyword 'SELECT'.Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 17Incorrect syntax near the keyword 'SELECT'.Msg 102, Level 15, State 1, Procedure GetPlayerAvg, Line 23Incorrect syntax near '@overAll'.Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 32Incorrect syntax near the keyword 'SELECT'.Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 42Incorrect syntax near the keyword 'SELECT'.
|
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-17 : 11:22:25
|
[code]declare @overAll as float,declare @physic as float,declare @technic as float[/code] KH |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 11:36:20
|
thanks a lot Peter and KH. here is the completed fixed code for all to see and useset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOalter FUNCTION [dbo].[GetPlayerAvg]( @playerid int,@calcType varchar(20) )RETURNS floatASBEGINdeclare @physic as float,@technic as float,@result as floatif @calcType='overall'beginSELECT @physic= ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13FROM PhisicalPropertiesTablewhere playerid=@playeridSELECT @technic= (Goalkeeper+ Corners+Crossing+Dribbling+Finishing+FirstTouch+Heading+LongShots+LongThrows+Marking+Passing+Penalty+Tackling+ThrowIns+ Aerial+AreaCommand+Communication+Handling+Kicking+OneOnOne+Reflex+Throwing+Positioning+OffBall+Creativity+Leadership+ShootingPower+ LongPass+Crosses)/29FROM TechnicalPropertiesTablewhere playerid=@playeridset @result=(@technic+@physic)/2endelse if @calcType='physic'beginSELECT @physic= ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13FROM PhisicalPropertiesTablewhere playerid=@playeridset @result=@physicendelse if @calcType='technic'begin SELECT @technic= (Goalkeeper+ Corners+Crossing+Dribbling+Finishing+FirstTouch+Heading+LongShots+LongThrows+Marking+Passing+Penalty+Tackling+ThrowIns+ Aerial+AreaCommand+Communication+Handling+Kicking+OneOnOne+Reflex+Throwing+Positioning+OffBall+Creativity+Leadership+ShootingPower+ LongPass+Crosses)/29FROM TechnicalPropertiesTablewhere playerid=@playeridset @result=@technicendreturn (@result)END |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 11:46:17
|
| Why are you averaging two averages?Also divide by 13.0 and 29.0 just in case there is no rounding.Peter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 11:52:23
|
| i need to calculate from two tables that represents the same player.but i think i get what you mean, i can do it all with one select on the two tables. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 12:06:39
|
set @result=(@technic+@physic)/2set @result = (@technic + @physic) / (13.0 + 29.0)Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 12:07:14
|
declare @overAll as float,declare @physic as float,declare @technic as floatPeter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 12:17:09
|
Thanks Peter Larsson,you are the greatest |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 12:17:18
|
Too many errors. Had to rewrite complete shebang.Try this ALTER FUNCTION dbo.GetPlayerAvg( @PlayerID INT, @CalcType VARCHAR(20))RETURNS FLOATASBEGIN DECLARE @Physic AS FLOAT, @Technic AS FLOAT IF @Calctype IN ('Overall', 'Physic', 'Technic') SELECT @Physic = [Work] + Weight + Height + RightFoot + LeftFoot + Strength + Stamina + Pace + Fitness + Jumping + Injury + Agility + Acceleration, @Technic = Goalkeeper + Corners + Crossing + Dribbling + Finishing + FirstTouch + Heading + LongShots + LongThrows + Marking + Passing + Penalty + Tackling + ThrowIns + Aerial + AreaCommand + Communication + Handling + Kicking + OneOnOne + Reflex + Throwing + Positioning + OffBall + Creativity + Leadership + ShootingPower + LongPass + Crosses FROM PhisicalPropertiesTable WHERE PlayerID = @PlayerID RETURN CASE WHEN @CalcType = 'Overall' THEN (@Physic + @Technic) / (13.0 + 29.0) WHEN @CalcType = 'Physic' THEN @Physic / 13.0 WHEN @CalcType = 'Technic' THEN @Technic / 29.0 ENDENDPeter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-17 : 12:32:53
|
| BTW,if i need to get the avg of more than one row should i get rid of the params @playerId?or there's a different way to do so. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-04-17 : 12:35:16
|
quote: Originally posted by lior3790 BTW,if i need to get the avg of more than one row should i get rid of the params @playerId?or there's a different way to do so.
Why don't you say so earlier ?  KH |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-17 : 12:46:37
|
| Please explain yourself.What more rows/records?Is it either one record (PlayerID) or all records?Peter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-18 : 03:54:06
|
Hi,i need both the one row option and for different use i need all AVG records of column playerid. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 04:02:01
|
If you want ALL records, pass @PlayerID as NULLALTER FUNCTION dbo.GetPlayerAvg( @PlayerID INT, @CalcType VARCHAR(20))RETURNS FLOATASBEGIN DECLARE @Physic AS FLOAT, @Technic AS FLOAT, @Records AS FLOAT IF @Calctype IN ('Overall', 'Physic', 'Technic') SELECT @Physic = SUM([Work] + Weight + Height + RightFoot + LeftFoot + Strength + Stamina + Pace + Fitness + Jumping + Injury + Agility + Acceleration), @Technic = SUM(Goalkeeper + Corners + Crossing + Dribbling + Finishing + FirstTouch + Heading + LongShots + LongThrows + Marking + Passing + Penalty + Tackling + ThrowIns + Aerial + AreaCommand + Communication + Handling + Kicking + OneOnOne + Reflex + Throwing + Positioning + OffBall + Creativity + Leadership + ShootingPower + LongPass + Crosses), @Records = COUNT(*) FROM PhisicalPropertiesTable WHERE PlayerID = COALESCE(@PlayerID, PlayerID) RETURN CASE WHEN @CalcType = 'Overall' THEN (@Physic + @Technic) / (13.0 + 29.0) WHEN @CalcType = 'Physic' THEN @Physic / 13.0 WHEN @CalcType = 'Technic' THEN @Technic / 29.0 END / @RecordsENDPeter LarssonHelsingborg, Sweden |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-04-18 : 04:12:16
|
Peter,Don't you think storing these aggregates in the variables which are later used to calculate average can be bad in multi-user environment.This can give wrong average due to phantom records scenario.It would be better to do it in single operation:ALTER FUNCTION dbo.GetPlayerAvg( @PlayerID INT, @CalcType VARCHAR(20))RETURNS FLOATASBEGIN DECLARE @Physic AS FLOAT, @Technic AS FLOAT, @Records AS FLOAT, @Result as Float Set @Result = 0 IF @Calctype IN ('Overall', 'Physic', 'Technic') Select @Result = CASE WHEN @CalcType = 'Overall' THEN (Physic + Technic) / (13.0 + 29.0) WHEN @CalcType = 'Physic' THEN Physic / 13.0 WHEN @CalcType = 'Technic' THEN Technic / 29.0 END / Records From ( SELECT Physic = SUM([Work] + Weight + Height + RightFoot + LeftFoot + Strength + Stamina + Pace + Fitness + Jumping + Injury + Agility + Acceleration), Technic = SUM(Goalkeeper + Corners + Crossing + Dribbling + Finishing + FirstTouch + Heading + LongShots + LongThrows + Marking + Passing + Penalty + Tackling + ThrowIns + Aerial + AreaCommand + Communication + Handling + Kicking + OneOnOne + Reflex + Throwing + Positioning + OffBall + Creativity + Leadership + ShootingPower + LongPass + Crosses), Records = COUNT(*) FROM PhisicalPropertiesTable WHERE PlayerID = COALESCE(@PlayerID, PlayerID) ) t RETURN(@Result)ENDHarsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-04-18 : 04:20:39
|
Why stop there? Why use variables at all?What if the player is not found? Division by zero?ALTER FUNCTION dbo.GetPlayerAvg( @PlayerID INT, @CalcType VARCHAR(20))RETURNS FLOATASBEGIN RETURN ( SELECT CASE WHEN @CalcType = 'Overall' THEN (x.Physic + x.Technic) / (13.0 + 29.0) WHEN @CalcType = 'Physic' THEN x.Physic / 13.0 WHEN @CalcType = 'Technic' THEN x.Technic / 29.0 END / NULLIF(x.Records, 0) FROM ( SELECT SUM([Work] + Weight + Height + RightFoot + LeftFoot + Strength + Stamina + Pace + Fitness + Jumping + Injury + Agility + Acceleration) AS Physic, SUM(Goalkeeper + Corners + Crossing + Dribbling + Finishing + FirstTouch + Heading + LongShots + LongThrows + Marking + Passing + Penalty + Tackling + ThrowIns + Aerial + AreaCommand + Communication + Handling + Kicking + OneOnOne + Reflex + Throwing + Positioning + OffBall + Creativity + Leadership + ShootingPower + LongPass + Crosses) AS Technic, COUNT(*) AS Records FROM PhisicalPropertiesTable WHERE @PlayerID IS NULL OR @PlayerID = PlayerID ) AS x )END Peter LarssonHelsingborg, Sweden |
 |
|
|
lior3790
Starting Member
46 Posts |
Posted - 2007-04-18 : 04:33:57
|
OK, these are good for calculating the whole table.I need to do it for the relative playerId in the whole.for example:playerid AVG1 5.52 8.43 12.14 6.35 2.7 |
 |
|
|
Next Page
|