![]() |
| |||||||
| Sponsored Links |
![]() |
| | LinkBack | Thread Tools | Display Modes |
|
#1
| ||||
| ||||
| Okay, I need some help with a SQL query. I have a DB with the following: Quote:
Some examples of data include: 1, 1, 10, Install Guide, 1 2, 1, 10, Install Guide, 2 3, 2, 12, Service Manual, 1 4, 2, 12, Service Manual, 2 5, 2, 12, Service Manual, 3 6, 1, 10, Install Guide, 3 So, the SQL would just select records 5 and 6, since those are unique manual types and are the most recent revisions.
__________________ "You'll never be as perfect as BLaaaaaaaaarche." |
| Sponsored Links |
|
#2
| ||||
| ||||
| See if this works: Code: SELECT DISTINCT(ManualTypeID),
ManualID,
ProductID,
ManualPartNumber,
ManualDescription,
ManualRevision
FROM tblManuals
ORDER BY ManualID DESC
__________________ jmurrayhead If you agree with me... click the icon! If my post solved your problem, click the button in the lower right-hand corner of the post.If you like it here...throw us a few bones to help support us. Join our Folding team: DeveloperBarn Folding |
|
#3
| ||||
| ||||
| That will not work because several different manuals can have the same ManualTypeID. I wonder if I just select based on unique ManualPartNumber? I don't think anyone can enter a duplicate Part Number... hmmm. Thanks for the clue. |
|
#4
| ||||
| ||||
| use MAX and GROUP BY Code: SELECT ManualTypeID, ProductID, ManualPartNumber, ManualDescription, MAX(ManualRevision) FROM tblManuals GROUP BY ManualTypeID, ProductID, ManualPartNumber, ManualDescription
__________________ Quote of the Month: Quality: The race for quality has no finish line- so technically, it's more like a death march. Questions to Ponder: What do you do when you see an endangered animal eating an endangered plant? iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm") copyright © 2008 sbenj69 |
|
#5
| ||||
| ||||
| The problem comes if you need the ManualID for some reason, since if you included that in your GROUP BY, you get every record (natch). To get the full record: Code: SELECT a.* FROM tblManuals A
JOIN
(SELECT ManualTypeId, Max(ManualRevision) as Latest
FROM tblManuals
GROUP BY ManualTypeId) B
ON (a.ManualTypeId = b.ManualTypeId
AND a.ManualRevision = b.Latest)
|
|
#6
| ||||
| ||||
| Okay, I am running into some difficulties. Here is my original SQL that grabs EVERY revision for each ManualPartNumber: Code: strSQL = "SELECT m.ManualID, m.ManualName, m.ManualApplication, m.ManualPartNumber, m.ManualSerialStart, m.ManualSerialEnd, m.ManualRevision, " & _ "m.ManualIteration, m.ManualPublished, m.ManualPublishedDate, u.UserName, p.ProductName, mt.ManualTypeName " & _ "FROM tblManuals m " & _ "LEFT JOIN tblUsers u ON m.ManualPublisher = u.UserID " & _ "LEFT JOIN tblProducts p ON m.ProductID = p.ProductID " & _ "LEFT JOIN tblManuals_types mt ON m.ManualTypeID = mt.ManualTypeID " & _ "WHERE 1 = 1" & _ strProdID & _ strManualTypeID |
|
#8
| ||||
| ||||
| Yes, but that means I need to GROUP BY all fields... right? |
|
#10
| ||||
| ||||
| you need to use all fields listed in your SELECT statement that are not part of an aggregate function (ie: MAX, SUM, COUNT, etc) in your GROUP BY statement. |
![]() |
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Connection String & Record Set Functions | mehere | ASP Code Samples | 0 | March 21st, 2008 12:07 PM |