Answers for "Created a simple table that adds up points scored per player per game, however I don't know what to do if a player didn't play in one of the games. Null seems to add a 0 value, which renders my calculations incorrect. Help, please."
https://ask.sqlservercentral.com/questions/119171/created-a-simple-table-that-adds-up-points-scored.html
The latest answers for the question "Created a simple table that adds up points scored per player per game, however I don't know what to do if a player didn't play in one of the games. Null seems to add a 0 value, which renders my calculations incorrect. Help, please."Answer by Kev Riley
https://ask.sqlservercentral.com/answers/119225/view.html
Try this:
declare @points table (Name varchar(100), GM1 int, GM2 int, GM3 int, GM4 int, GM5 int)
insert into @points select 'Dell Favors',10, null, 15, 2, 0
insert into @points select 'Ron Elliot',10, 0, 15, 2, 0
select
Name,
GM1,
GM2,
GM3,
GM4,
GM5,
isnull(GM1,0)+isnull(GM2,0)+isnull(GM3,0)+isnull(GM4,0)+isnull(GM5,0) as TotalPoints,
(select count(Points) from (values (GM1),(GM2),(GM3),(GM4),(GM5)) as Game(Points) ) as GamesPlayed,
(isnull(GM1,0)+isnull(GM2,0)+isnull(GM3,0)+isnull(GM4,0)+isnull(GM5,0) ) * 1.0
/
(select count(Points) from (values (GM1),(GM2),(GM3),(GM4),(GM5)) as Game(Points) ) as PPG
from @points
I use isnull() around each points score to prevent null values from making the total null, and then pivot the scores around into rows from columns so that I can count how many there are. COUNT by default will only count non-null values, so in this case it doesn't count the games where no score exists.
This results in
Name GM1 GM2 GM3 GM4 GM5 TotalPoints GamesPlayed PPG
-------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ---------------
Dell Favors 10 NULL 15 2 0 27 4 6.750000000000
Ron Elliot 10 0 15 2 0 27 5 5.400000000000Thu, 26 Mar 2015 15:58:04 GMTKev RileyAnswer by Database_Novice
https://ask.sqlservercentral.com/answers/119222/view.html
Sure. Now keep in mind that this table is not normalized, so I apologize for the remedial database structure. However, in this particular scenario, I would just like to know how to address handling players who may have not played in a game due to injury or had a BYE week. But still compute their average points per game. Thanks for your help. I greatly appreciate it. I am new to this.
Player Name Team Name GM1_Points GM2_Points GM3_Points GM4_Points GM5_Points PPG
Dell Favors DNP 10 - 15 2 0 Calculation (GM1_Points + GM2_Points + GM3_Points + GM4_Points + GM5_Points/5)
Ron Eilliott DNP 10 0 15 2 0 Calculation (GM1_Points + GM2_Points + GM3_Points + GM4_Points + GM5_Points/5)
In the example below, this player (Dell Favors) did not play in Game 2, so the data shouldn't be used to calculate his average. However, Player 2 (Ron Eilliott)played in Game 2, he just didn't score any points that game, so his game point total would be used to calculate his average.
Can you create unique functions for each row as you can for a specific column?
I am able to use the Calculation (GM1_Points + GM2_Points + GM3_Points + GM4_Points + GM5_Points/5) calculation to insert into the PPG column however if each record has, as in the example above details, a different number of games that need to be used in the calculation, the column calculation is not accurate.
Dell Favors has played in 4 games but Ron Eilliott has played in 5 games, therefore the formula needs to account for this.Thu, 26 Mar 2015 15:30:11 GMTDatabase_Novice