Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Databases > SQL Development

Sponsored Links

Discuss "Select Top 1 Unique Record" in the SQL Development forum.

SQL Development - Structured Query Language (SQL) is the talk of databases. Discuss topics such as joins, triggers and other advanced SQL topics.


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1  
Old April 16th, 2008, 08:48 AM
BLaaaaaaaaaarche's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 55
Thanks: 10
Thanked 7 Times in 5 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
HTML & CSS Classic ASP 
Total Awards: 2

Default Select Top 1 Unique Record

Okay, I need some help with a SQL query. I have a DB with the following:

Quote:
Table: tblManuals
Fields: ManualID, ManualTypeID, ProductID, ManualPartNumber, ManualDescription, ManualRevision
For each manual, it could have multiple revisions. The way I identify each manual is by the part number, type ID, and product ID. How do I select just the MOST RECENT manual for each revision (in other words, just the most recent revision of the manual).

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  
Old April 16th, 2008, 09:00 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 952
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 6
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

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  
Old April 16th, 2008, 09:06 AM
BLaaaaaaaaaarche's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 55
Thanks: 10
Thanked 7 Times in 5 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
HTML & CSS Classic ASP 
Total Awards: 2

Default

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  
Old April 16th, 2008, 09:07 AM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 160
Thanks: 12
Thanked 29 Times in 27 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

use MAX and GROUP BY
Code:
SELECT ManualTypeID, ProductID, ManualPartNumber, ManualDescription, MAX(ManualRevision) FROM tblManuals GROUP BY ManualTypeID, ProductID, ManualPartNumber, ManualDescription
if you need to also get the ManualID ... you would have to go another way by using a subselect ... let me know if that's how you need to go.

Comments on this post
jmurrayhead agrees: why didn't I think of that? Probably because I don't use it enough
__________________
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  
Old April 16th, 2008, 09:55 AM
Wolffy's Avatar
Slaprentice of Wolves
 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 176
Thanks: 3
Thanked 24 Times in 21 Posts
Rep Power: 1
Wolffy is on a distinguished road

Awards Showcase
Microsoft .Net 
Total Awards: 1

Default

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)

Comments on this post
jmurrayhead agrees:
BLaaaaaaaaaarche agrees: Sweet.
  #6  
Old April 16th, 2008, 10:07 AM
BLaaaaaaaaaarche's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 55
Thanks: 10
Thanked 7 Times in 5 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
HTML & CSS Classic ASP 
Total Awards: 2

Default

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
I keep on getting an error when trying to add the MAX to the query. Basically, I just need to select the MAX ManualRevision for each unique ManualPartNumber.
  #7  
Old April 16th, 2008, 10:13 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 952
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 6
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

You need to add the Group By clause when using MAX.
  #8  
Old April 16th, 2008, 10:27 AM
BLaaaaaaaaaarche's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 55
Thanks: 10
Thanked 7 Times in 5 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
HTML & CSS Classic ASP 
Total Awards: 2

Default

Yes, but that means I need to GROUP BY all fields... right?
  #9  
Old April 16th, 2008, 10:36 AM
jmurrayhead's Avatar
The Barnfather
 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 952
Thanks: 22
Thanked 93 Times in 90 Posts
Blog Entries: 6
Rep Power: 4
jmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the roughjmurrayhead is a jewel in the rough

Awards Showcase
Microsoft Windows Microsoft SQL Server Microsoft .Net Classic ASP 
Total Awards: 4

Default

Quote:
Originally Posted by BLaaaaaaaaaarche View Post
Yes, but that means I need to GROUP BY all fields... right?
I would think you would only need to group by ManualPartNumber in this case.
  #10  
Old April 16th, 2008, 11:04 AM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 160
Thanks: 12
Thanked 29 Times in 27 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

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.
Closed Thread

  DeveloperBarn Forums > Databases > SQL Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

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


All times are GMT -4. The time now is 10:06 AM.



Content Relevant URLs by vBSEO 3.2.0