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

max date and only 1 record per toolnumber

I am trying to get 1 maxdate from a table foreach toolnumber inserted but I keep getting multiple for each tool number. There should be 1 row returned for toolnumber 1, 1 for toolnumber 2, 1 for toolnumber 3 etc..........
SELECT 
a.UploadTime
      ,a.[ToolNumber]
      ,a.[ToolSize]
      ,a.[ToolStyle]
      ,a.[DefaultLocationIndex]  
      FROM ToolAssemblies a
      INNER JOIN( SELECT MAX(UploadTime) as UploadTime   ,[ToolNumb[embed=file 905894]er]
      ,[ToolSize]
      ,[ToolStyle]
      ,[DefaultLocationIndex]  FROM ToolAssemblies WHERE ToolNumber IN ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 24 , 25 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 39 , 40 , 47 , 51 , 56 , 66 , 71 , 76 , 79 , 82 ) GROUP BY ToolNumber, ToolSize,ToolStyle,DefaultLocationIndex ) b 
      ON a.UploadTime = b.UploadTime
      ORDER BY ToolNumber

Open in new window

Capture.PNG
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
r3nder

8/22/2022 - Mon
dsacker

Do you even need the additional INNER JOIN on the same table?
SELECT 
       a.[ToolNumber]
      ,a.[ToolSize]
      ,a.[ToolStyle]
      ,a.[DefaultLocationIndex]
      , MAX(UploadTime) AS UploadTime
FROM ToolAssemblies a
WHERE ToolNumber IN ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 , 13 , 14 , 16 , 17 , 18 , 19 , 20 , 21 , 22 , 24 , 25 , 27 , 28 , 29 , 30 , 31 , 32 , 33 , 34 , 35 , 36 , 37 , 39 , 40 , 47 , 51 , 56 , 66 , 71 , 76 , 79 , 82 )
GROUP BY a.[ToolNumber]
      ,a.[ToolSize]
      ,a.[ToolStyle]
      ,a.[DefaultLocationIndex]
ORDER BY ToolNumber

Open in new window

ASKER CERTIFIED SOLUTION
Aneesh

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
Perfect Thank you Aneesh
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes