0

I am trying to write a SQL query (In SQL Server) to find the shortest player drafted each year by the NBA.

How can I fix this issue? Is there a better way to write the query?

I am trying with the following query:

SELECT a.player_height, a.player_name,  a.draft_year
FROM NBA.dbo.Players a
WHERE a.draft_year IS NOT NULL
GROUP BY a.draft_year
ORDER BY a.draft_year;

player_name is datatype nvarchar, draft_year is float, player_height is float.

Of course, I am getting the error:

Column 'NBA..Players.player_height' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

I need to get the shortest player's name and height for EACH year of the draft. The question that was flagged as associated is only telling how to get the top row of a query.

coverc23
  • 1
  • 2
  • Year and height should definitely not be float, they should be int and decimal. – Dale K Mar 22 '23 at 00:48
  • I've tried this: SELECT MIN(CONVERT(decimal, a.player_height)), a.player_name, CONVERT(int, a.draft_year) FROM NBA.dbo.Players a WHERE a.draft_year IS NOT NULL GROUP BY a.draft_year ORDER BY a.draft_year; but it is still giving the same area – coverc23 Mar 22 '23 at 00:53
  • See the duplicate – Dale K Mar 22 '23 at 00:58
  • Try: select * from (select *, row_number() over(partition by draft_year order by height) AS sort from NBA.dbo.Players a WHERE a.draft_year IS NOT NULL) x where x.sort = 1 – siggemannen Mar 22 '23 at 01:14

0 Answers0