Solved

MAX(UID) in MySQL

Posted on 2015-02-05
8
415 Views
Last Modified: 2015-02-05
I am trying to get the max unique identifier from a table
and I keep getting the first id not the last
Select MAX(UID) as id,UploadDate, Tooltype,SerialNumber,LocationID as 'locid' FROM Inventory_SerializedAssets Where LocationID in(" + otherlocations + ") AND ToolType in ('CO','PT','SS','BG','SP','TN') GROUP BY uid ORDER BY id

Open in new window

0
Comment
Question by:r3nder
  • 5
  • 2
8 Comments
 
LVL 18

Assisted Solution

by:Simon
Simon earned 250 total points
ID: 40591622
You don't need to GROUP BY UID if you want the MAX value for it.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 250 total points
ID: 40591651
I'm not an expert on MySQL, but I think this is what you need:


SELECT UID AS id, UploadDate, Tooltype, SerialNumber, LocationID AS 'locid'
FROM Inventory_SerializedAssets
WHERE LocationID in(" + otherlocations + ") AND
      ToolType in ('CO','PT','SS','BG','SP','TN')
ORDER BY UID DESC
LIMIT 1
0
 
LVL 6

Author Comment

by:r3nder
ID: 40591673
no, sorry that didnt work maybe if  I show you the data

UID | UploadDate              |LocID|SerialNumber|TOOLTYPE|QTY|NOTES                                                 |UserID
842 |2015-02-03 04:22:33|0        |1054                 |CO             |1     |Shipped from Dist 1, received by...|14
843 |2015-02-04 04:27:34|3        |1054                 |CO             |1     |Shipped from Dist 2, received by...|7 <---------This is the one I want
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 6

Author Comment

by:r3nder
ID: 40591683
842 |2015-02-03 04:22:33|0        |1054                 |CO             |1     |Shipped from Dist 1, received by...|14
843 |2015-02-04 04:27:34|3        |1054                 |CO             |1     |Shipped from Dist 2, received by...|7 <---------This is the one I want
546 |2015-01-03 04:22:33|0        |1022                 |CO             |1     |Shipped from Dist 17, received by...|14
588|2015-01-04 04:27:34|3        |1022                 |CO             |1     |Shipped from Dist 2, received by...|7 <---------This is the one I want
etc.............
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40591712
So rather than just the max UID, you need the max UID per SerialNumber?  Or by what criteria?
0
 
LVL 6

Author Comment

by:r3nder
ID: 40591731
I need the max(UID)  for each serialnumber that matches WHERE LocationID in(3,2,1,4) AND
      ToolType in ('CO','PT','SS','BG','SP','TN')
0
 
LVL 6

Author Comment

by:r3nder
ID: 40592107
I figured it out
SELECT UID AS id,
              UploadDate,
              Tooltype,
              SerialNumber,
              LocationID AS 'locid'
FROM Inventory_SerializedAssets
WHERE UID IN(SELECT MAX(UID) FROM Inventory_SerializedAssets GROUP BY SerialNumber)  
              AND LocationID in(" + otherlocations + ")
              AND ToolType in ('CO','PT','SS','BG','SP','TN') GROUP BY SerialNumber
0
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40592110
Thanks for the help
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question