Column does not exist
When using WHERE as a filter condition, it is important to think about the processing order in the query. In this exercise, you want a query that returns NBA players with average total rebounds of 12 or more per game. The following formula calculates average total rebounds from the PlayerStats table;
$$Average Total Rebounds = \dfrac{(Defensive Rebounds + Offensive Rebounds)}{Games Played}$$
The first query in Step 1 returns an error. Select Run Code to view the error. The second query, in Step 2, will give you the results you want, without error, by using a sub-query.
Note that GamesPlayed is CAST AS numeric to ensure we get decimal points in our output, as opposed to whole numbers.
Latihan ini adalah bagian dari kursus
Improving Query Performance in SQL Server
Latihan interaktif praktis
Cobalah latihan ini dengan menyelesaikan kode contoh berikut.
-- First query
SELECT PlayerName,
Team,
Position,
(DRebound+ORebound)/CAST(GamesPlayed AS numeric) AS AvgRebounds
FROM PlayerStats
WHERE AvgRebounds >= 12;