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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to get AVG of a row?

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.0
FROM Table1



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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?
Go to Top of Page

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

Go to Top of Page

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 ON
set QUOTED_IDENTIFIER ON
GO
alter FUNCTION [dbo].[GetPlayerAvg]
( @playerid int,@calcType varchar(20) )
RETURNS float
AS
BEGIN
declare @overAll as float,
declare @physic as float,
declare @technic as float


if @calcType='overall'
begin
set @physic=SELECT ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13
FROM PhisicalPropertiesTable
where playerid=@playerid

set @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)/29
FROM TechnicalPropertiesTable
where playerid=@playerid

@overAll=(@technic+@physic)/2

return (@overAll)

end

else if @calcType='physic'
begin

set @physic=SELECT ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13
FROM PhisicalPropertiesTable
where playerid=@playerid
return(@physic)


end

else if @calcType='technic'
begin
set @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)/29
FROM TechnicalPropertiesTable
where playerid=@playerid
return (@technic)

end


END


quote:
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 7
Incorrect syntax near the keyword 'declare'.
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 8
Incorrect syntax near the keyword 'declare'.
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 13
Incorrect syntax near the keyword 'SELECT'.
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 17
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Procedure GetPlayerAvg, Line 23
Incorrect syntax near '@overAll'.
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 32
Incorrect syntax near the keyword 'SELECT'.
Msg 156, Level 15, State 1, Procedure GetPlayerAvg, Line 42
Incorrect syntax near the keyword 'SELECT'.
Go to Top of Page

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

Go to Top of Page

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 use

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
alter FUNCTION [dbo].[GetPlayerAvg]
( @playerid int,@calcType varchar(20) )
RETURNS float
AS
BEGIN
declare @physic as float,
@technic as float,
@result as float


if @calcType='overall'
begin
SELECT @physic= ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13
FROM PhisicalPropertiesTable
where playerid=@playerid

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)/29
FROM TechnicalPropertiesTable
where playerid=@playerid

set @result=(@technic+@physic)/2


end

else if @calcType='physic'
begin

SELECT @physic= ([Work]+ Weight+ Height+ RightFoot+ LeftFoot+ Strength+ Stamina+ Pace+ Fitness+ Jumping+ Injury+ Agility+ Acceleration)/13
FROM PhisicalPropertiesTable
where playerid=@playerid
set @result=@physic


end

else 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)/29
FROM TechnicalPropertiesTable
where playerid=@playerid
set @result=@technic

end
return (@result)

END



Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-17 : 12:06:39
set @result=(@technic+@physic)/2

set @result = (@technic + @physic) / (13.0 + 29.0)



Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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 float


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

lior3790
Starting Member

46 Posts

Posted - 2007-04-17 : 12:17:09

Thanks Peter Larsson,

you are the greatest

Go to Top of Page

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 FLOAT
AS
BEGIN
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
END
END

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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.
Go to Top of Page

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

Go to Top of Page

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 Larsson
Helsingborg, Sweden
Go to Top of Page

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.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-04-18 : 04:02:01
If you want ALL records, pass @PlayerID as NULL
ALTER FUNCTION dbo.GetPlayerAvg
(
@PlayerID INT,
@CalcType VARCHAR(20)
)
RETURNS FLOAT
AS
BEGIN
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 / @Records
END


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

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 FLOAT
AS
BEGIN
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)
END


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

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 FLOAT
AS
BEGIN
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 Larsson
Helsingborg, Sweden
Go to Top of Page

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      AVG
1 5.5
2 8.4
3 12.1
4 6.3
5 2.7
Go to Top of Page
    Next Page

- Advertisement -