Avatar of r3nder
r3nder
Flag for United States of America asked on

select and count the max id

I have a query where I am trying to count the max id that meets this criteria
Select Count(ID),MAX(ID)  from  ToolAssemblies WHERE ToolNumber =52  
This should come up as 1 but it counts 29?
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Jim Horn

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
r3nder

ASKER
got the idea - thanks Jim

SELECT COUNT(ID)
FROM ToolAssemblies
WHERE id IN (Select MAX(ID)as max_id from  ToolAssemblies WHERE ToolNumber =52 )
Jim Horn

<slight cleanup, don't need an alias with an IN clause>
SELECT COUNT(id) 
FROM ToolAssemblies
WHERE id IN (Select MAX(ID) from ToolAssemblies WHERE ToolNumber = 52 )

Open in new window

Thanks for the grade.  Good luck with your project.  -Jim
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23