I have the following two relationships:
Game(id, name, year) Devs(pid, gid, role)
Where Game.id is the primary key and where Devs .gid is the foreign key for Game.id.
In the previous post I made here , another user was kind enough to help me create a query that finds all the games made with most developers making this game. His answer used the WITH statement, and I'm not very good at them. since Iβve been studying SQL for only a few weeks. Here's a working request:
WITH GamesDevs (GameName, DevsCount) AS ( SELECT Game.name AS GameName, count(DISTINCT Devs.pid) AS DevsCount FROM Game, Devs WHERE Devs.gid=Game.id GROUP BY Devs.gid, Game.name ) SELECT * FROM GamesDevs WHERE GamesDevs.DevsCount = (SELECT MAX(DevsCount) FROM GamesDevs)
In order to get acquainted with SQL, I am trying to rewrite this query using a subquery, not a WITH statement. I used this Oracle documentation to help me figure it out. I tried to rewrite the request as follows:
SELECT * FROM (SELECT Game.name AS GameName, count(DISTINCT Devs.pid) AS DevsCount FROM Game, Devs WHERE Devs.gid=Game.id GROUP BY Devs.gid, Game.name) GamesDevs WHERE GamesDevs.DevsCount = (SELECT MAX(DevsCount) FROM GamesDevs)
As far as I can tell, these two queries should be the same. However, when I try to run the second request, I get an error
Msg 207 Level 16 State 1 Line 6 Invalid column name 'DevsCount'.
Does anyone know why I can get this error or why these two queries will not be identical?
sql with-statement azure-sql-database
Kyle
source share