Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SELECT result in another SELECT

Tags:

sql

select

So here is my query

SELECT      *  FROM      Score  AS NewScores  WHERE      InsertedDate >= DATEADD(mm, -3, GETDATE());   SELECT      ROW_NUMBER() OVER( ORDER BY NETT) AS Rank,      Name,      FlagImg,      Nett,      Rounds  FROM (     SELECT          Members.FirstName + ' ' + Members.LastName AS Name,          CASE              WHEN MenuCountry.ImgURL IS NULL THEN                  '~/images/flags/ismygolf.png'              ELSE                  MenuCountry.ImgURL          END AS FlagImg,          AVG(CAST(NewScores.NetScore AS DECIMAL(18, 4))) AS Nett,          COUNT(Score.ScoreID) AS Rounds      FROM          Members          INNER JOIN          Score              ON Members.MemberID = Score.MemberID          LEFT OUTER JOIN MenuCountry              ON Members.Country = MenuCountry.ID      WHERE          Members.Status = 1      GROUP BY          Members.FirstName + ' ' + Members.LastName,          MenuCountry.ImgURL     ) AS Dertbl  ORDER BY;      

The query is to give a result set for a GridView based leaderboard and what I want is to only get the average of Scores that are less than 3 months old. I have this in 2 parts as you can see and obviously it gives an error like this.

Msg 4104, Level 16, State 1, Line 2

The multi-part identifier "NewScores.NetScore" could not be bound.

Which is because of this AVG(CAST(NewScores.NetScore AS DECIMAL(18, 4))) AS Nett

How do I make it so that I can use NewScores there so I'm only getting the average of the scores less than 3 months old?

EDIT: Using the answers people provided I've solved it by using a join in the correct place and here is the correct query:

SELECT ROW_NUMBER() OVER(ORDER BY NETT) AS Rank, Name, FlagImg, Nett, Rounds FROM (SELECT Members.FirstName + ' ' + Members.LastName AS Name, CASE WHEN MenuCountry.ImgURL IS NULL THEN '~/images/flags/ismygolf.png' ELSE MenuCountry.ImgURL END AS FlagImg, AVG(CAST(NewScores.NetScore AS DECIMAL(18, 4))) AS Nett, COUNT(NewScores.ScoreID) AS Rounds FROM Members INNER JOIN (SELECT * FROM Score WHERE InsertedDate >= DATEADD(mm, -5, GETDATE())) NewScores ON Members.MemberID = NewScores.MemberID LEFT OUTER JOIN MenuCountry ON Members.Country = MenuCountry.ID WHERE Members.Status = 1 GROUP BY Members.FirstName + ' ' + Members.LastName, MenuCountry.ImgURL) AS Dertbl ORDER BY Nett ASC 
like image 956
Pandepic Avatar asked Apr 12 '13 00:04

Pandepic


People also ask

How can I use one query result in another query?

Use the results of a query as a field in another query. You can use a subquery as a field alias. Use a subquery as a field alias when you want to use the subquery results as a field in your main query. Note: A subquery that you use as a field alias cannot return more than one field.

How do I SELECT within a SELECT in SQL?

The subquery can be nested inside a SELECT, INSERT, UPDATE, or DELETE statement or inside another subquery. A subquery is usually added within the WHERE Clause of another SQL SELECT statement. You can use the comparison operators, such as >, <, or =.

Can we use SELECT * with group by?

Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause. The original idea was to create the table in beginning of the query, so the (SELECT * FROM #TBL) could be used on the query itself, instead of defining the names on each GROUP BY.


1 Answers

NewScores is an alias to Scores table - it looks like you can combine the queries as follows:

SELECT      ROW_NUMBER() OVER( ORDER BY NETT) AS Rank,      Name,      FlagImg,      Nett,      Rounds  FROM (     SELECT          Members.FirstName + ' ' + Members.LastName AS Name,          CASE              WHEN MenuCountry.ImgURL IS NULL THEN                  '~/images/flags/ismygolf.png'              ELSE                  MenuCountry.ImgURL          END AS FlagImg,          AVG(CAST(NewScores.NetScore AS DECIMAL(18, 4))) AS Nett,          COUNT(Score.ScoreID) AS Rounds      FROM          Members          INNER JOIN          Score NewScores             ON Members.MemberID = NewScores.MemberID          LEFT OUTER JOIN MenuCountry              ON Members.Country = MenuCountry.ID      WHERE          Members.Status = 1          AND NewScores.InsertedDate >= DATEADD(mm, -3, GETDATE())     GROUP BY          Members.FirstName + ' ' + Members.LastName,          MenuCountry.ImgURL     ) AS Dertbl  ORDER BY; 
like image 182
John D Avatar answered Sep 23 '22 10:09

John D