Suppose I have a two tables. Table T1
like:
Game Player Points Assists
1 1 10 5
1 2 5 10
T2
like:
Game Player Fouls Turnovers
1 1 5 5
1 2 10 10
I'd like to create a view with one row per player and a new field rating
, where rating
is a equally weighted sum of Points, Assists, Fouls,Turnovers
for each player. (i.e. rating = .25 * Points + .25 * Assists + .25 * Fouls + .25 * Turnovers)
I create the view:
CREATE VIEW `player_view` AS (
SELECT Player,
SUM(
Points *0.25 +
Assists *0.25 +
Fouls *0.25 +
Turnovers *0.25) AS Rating)
FROM T1 INNER JOIN T2 ON
T1.Player = T2.Player
AND T1.Game = T2.Game
GROUP BY Player
But rather than returning a value, I get NULL
for all Rating
:
Player Rating
1 NULL
2 NULL
Initially, I was faced with
Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'support_desk.mod_users_groups.group_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
So I disabled only_full_group_by
via SET sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'
So now, although the view returns a results set, the Ratings
are all NULL. Please assist.
Seems that you missed an operator between the column in sum eg + could be you ahve some hidden char in the key fields so.. try trim()
CREATE VIEW `player_view` AS
SELECT Player,
SUM(
t1.Points*0.25 +
t1.Assists*0.25 +
t2.Fouls*0.25 +
t2.Turnovers*0.25) AS Rating
)
FROM T1
INNER JOIN T2 ON
trim(T1.Player) = trim(T2.Player)
AND trim(T1.Game) = trim(T2.Game);
GROUP BY Player
select * from player_view;