return 1 row for each toolnumber

I am trying to return the max(uploadtime for each serialnumber along with other data
I have kind of achieved this but it returns the same toolnumber multiple times with the same maxdate  - data and image attached
SELECT toolassemblies.uploadtime                         AS 'BuildDate', 
       toolassemblies.toolnumber, 
       l.location                                        AS 'Location', 
       'Tool Communication'                              AS 'Event', 
       batterygaugesn, 
       ( 'Tool Size: ' + Cast(toolsize AS VARCHAR) 
         + '; ' + 'Controller: ' 
         + Cast(controllersn AS VARCHAR) + '; ' 
         + 'Battery Gauge: ' 
         + Cast(batterygaugesn AS VARCHAR) + '; ' 
         + 'Battery: ' + Cast(batterysn AS VARCHAR) 
         + '; ' + 'Magnetics: ' 
         + Cast(magneticssn AS VARCHAR) + ' Used: ' 
         + Cast(Round(bd.batteryused, 2)AS VARCHAR) 
         + ';' + ' Voltage: ' 
         + Cast(Round(bd.batteryvoltage, 2)AS VARCHAR) ) AS 'Event Notes' 
FROM   toolassemblies 
       LEFT JOIN locations AS l 
              ON l.locationindex = toolassemblies.defaultlocationindex 
       LEFT JOIN batterydata AS bd 
              ON bd.defaultlocationindex = toolassemblies.defaultlocationindex 
WHERE  toolassemblies.toolnumber IN (SELECT DISTINCT toolnumber 
                                     FROM   toolassemblies) 
       AND bd.gaugeserialnumber = batterygaugesn 
       AND toolassemblies.uploadtime = (SELECT Max(uploadtime) 
                                        FROM   toolassemblies b 
                                        WHERE 
           toolassemblies.toolnumber = b.toolnumber 
                                       ) 
       AND toolassemblies.uploadtime BETWEEN 
           '2012-01-31 11:59:59 PM' AND '2015-03-31 12:00:00 AM' 
ORDER  BY toolassemblies.toolnumber   

Open in new window

data.csv
datapic
LVL 6
r3nderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TalokEchelonCommented:
Rather than perform the Max(uploadtime) in the WHERE clause, you could put it as an inline SELECT and see how that works for you.  This would select the Max Uploadtime for the toolnumber in the row returned in the joined rowset.

Also, the first WHERE clause looks a little redundant.  toolassemblies.toolnumber will always be in (SELECT DISTINCT toolnumber FROM toolassemblies).  If it were a SELECT DISTINCT on a different table, say toolassemblyhistory.toolnumber, it would make more sense.  As written, it will always return every toolnumber in toolassemblies, which is what you had to begin with.  It is not reducing the rowset any.

SELECT (SELECT Max(uploadtime) 
          FROM   toolassemblies b 
          WHERE toolassemblies.toolnumber = b.toolnumber 
          )                         AS 'BuildDate', 
       toolassemblies.toolnumber, 
       l.location                                        AS 'Location', 
       'Tool Communication'                              AS 'Event', 
       batterygaugesn, 
       ( 'Tool Size: ' + Cast(toolsize AS VARCHAR) 
         + '; ' + 'Controller: ' 
         + Cast(controllersn AS VARCHAR) + '; ' 
         + 'Battery Gauge: ' 
         + Cast(batterygaugesn AS VARCHAR) + '; ' 
         + 'Battery: ' + Cast(batterysn AS VARCHAR) 
         + '; ' + 'Magnetics: ' 
         + Cast(magneticssn AS VARCHAR) + ' Used: ' 
         + Cast(Round(bd.batteryused, 2)AS VARCHAR) 
         + ';' + ' Voltage: ' 
         + Cast(Round(bd.batteryvoltage, 2)AS VARCHAR) ) AS 'Event Notes' 
FROM   toolassemblies 
       LEFT JOIN locations AS l 
              ON l.locationindex = toolassemblies.defaultlocationindex 
       LEFT JOIN batterydata AS bd 
              ON bd.defaultlocationindex = toolassemblies.defaultlocationindex 
WHERE  toolassemblies.toolnumber IN (SELECT DISTINCT toolnumber 
                                     FROM   toolassemblies) 
       AND bd.gaugeserialnumber = batterygaugesn  
       AND toolassemblies.uploadtime BETWEEN 
           '2012-01-31 11:59:59 PM' AND '2015-03-31 12:00:00 AM' 
ORDER  BY toolassemblies.toolnumber   

Open in new window

0
r3nderAuthor Commented:
@Talok good suggestions but the max upload in the select clause slowed it down to a 4 1/2 minute query - I do agree that the toolassemblies.toolnumber IN (SELECT DISTINCT toolnumber
                                     FROM   toolassemblies)  
was redundant so I took it out and put the max back in the where clause and it runs at 25 seconds but still has multiple rows
0
TalokEchelonCommented:
Are you only wanting to return the last batterygaugesn used for a particular toolnumber with the last uploadtime?
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

r3nderAuthor Commented:
now it is less than a second but still multiple rows for each toolnumber

SELECT DISTINCT  toolassemblies.toolnumber,
toolassemblies.uploadtime                         AS 'BuildDate', 
       
       l.location                                        AS 'Location', 
       'Tool Communication'                              AS 'Event', 
       batterygaugesn, 
       ( 'Tool Size: ' + Cast(toolsize AS VARCHAR) 
         + '; ' + 'Controller: ' 
         + Cast(controllersn AS VARCHAR) + '; ' 
         + 'Battery Gauge: ' 
         + Cast(batterygaugesn AS VARCHAR) + '; ' 
         + 'Battery: ' + Cast(batterysn AS VARCHAR) 
         + '; ' + 'Magnetics: ' 
         + Cast(magneticssn AS VARCHAR) + ' Used: ' 
         + Cast(Round(bd.batteryused, 2)AS VARCHAR) 
         + ';' + ' Voltage: ' 
         + Cast(Round(bd.batteryvoltage, 2)AS VARCHAR) ) AS 'Event Notes' 
FROM   toolassemblies 
       LEFT JOIN locations AS l 
              ON l.locationindex = toolassemblies.defaultlocationindex 
       LEFT JOIN batterydata AS bd 
              ON bd.defaultlocationindex = toolassemblies.defaultlocationindex 
WHERE  
       bd.gaugeserialnumber = batterygaugesn  
        AND toolassemblies.uploadtime = (SELECT Max(uploadtime) 
                                        FROM   toolassemblies b 
                                        WHERE 
           toolassemblies.toolnumber = b.toolnumber 
                                       ) 
       AND toolassemblies.uploadtime BETWEEN 
           '2012-01-31 11:59:59 PM' AND '2015-03-31 12:00:00 AM' 
           AND l.LocationIndex = 0
ORDER  BY toolassemblies.toolnumber 

Open in new window

0
r3nderAuthor Commented:
basically everything in the event notes for each toolnumber
0
TalokEchelonCommented:
OK.  As written, it will return a row for every batterydata row that matches in your join.  If you're looking to concatenate the Event Notes rows into a single column in a toolnumber row, you can explore using a recursive Common Table Expression (CTE), FOR XML statement, or a "dreaded" SQL Cursor to do it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r3nderAuthor Commented:
used CTE thanks Talok
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.