Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MAX(UID) in MySQL

Posted on 2015-02-05
8
Medium Priority
?
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
8 Comments
 
LVL 18

Assisted Solution

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

Accepted Solution

by:
Scott Pletcher earned 1000 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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 
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 70

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

721 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