DeveloperBarn Forums

Go Back   DeveloperBarn Forums > Databases > SQL Development

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 « Previous Thread | Next Thread »
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old April 16th, 2008, 08:48 AM
BLaaaaaaaaaarche's Avatar
Moderator

 
Join Date: Mar 2008
Posts: 43
Thanks: 10
Thanked 6 Times in 4 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
Classic ASP 
Total Awards: 1

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 (permalink)  
Old April 16th, 2008, 09:00 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 530
Thanks: 14
Thanked 38 Times in 37 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead is on a distinguished road

Awards Showcase
Microsoft SQL Server Microsoft Windows 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
Did I help you out? Make me popular by clicking the icon!

If you found a post helpful, please click the button in the lower right-hand corner of the post.

Powered by ASP.Net
  #3 (permalink)  
Old April 16th, 2008, 09:06 AM
BLaaaaaaaaaarche's Avatar
Moderator

 
Join Date: Mar 2008
Posts: 43
Thanks: 10
Thanked 6 Times in 4 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
Classic ASP 
Total Awards: 1

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 (permalink)  
Old April 16th, 2008, 09:07 AM
mehere's Avatar
Mistress of Sarcasm

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 111
Thanks: 8
Thanked 19 Times in 17 Posts
Rep Power: 1
mehere is on a distinguished road

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:
Strife: As long as we have each other, we'll never run out of problems.

Questions to Ponder:
Should vegetarians eat animal crackers?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
  #5 (permalink)  
Old April 16th, 2008, 09:55 AM
Wolffy's Avatar
Slaprentice of Wolves


 
Join Date: Mar 2008
Location: Peoria, IL
Posts: 146
Thanks: 1
Thanked 23 Times in 20 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 (permalink)  
Old April 16th, 2008, 10:07 AM
BLaaaaaaaaaarche's Avatar
Moderator

 
Join Date: Mar 2008
Posts: 43
Thanks: 10
Thanked 6 Times in 4 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
Classic ASP 
Total Awards: 1

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 (permalink)  
Old April 16th, 2008, 10:13 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 530
Thanks: 14
Thanked 38 Times in 37 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead is on a distinguished road

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

Default

You need to add the Group By clause when using MAX.
  #8 (permalink)  
Old April 16th, 2008, 10:27 AM
BLaaaaaaaaaarche's Avatar
Moderator

 
Join Date: Mar 2008
Posts: 43
Thanks: 10
Thanked 6 Times in 4 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
Classic ASP 
Total Awards: 1

Default

Yes, but that means I need to GROUP BY all fields... right?
  #9 (permalink)  
Old April 16th, 2008, 10:36 AM
jmurrayhead's Avatar
Your Lord & Master

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 530
Thanks: 14
Thanked 38 Times in 37 Posts
Blog Entries: 2
Rep Power: 1
jmurrayhead is on a distinguished road

Awards Showcase
Microsoft SQL Server Microsoft Windows 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 (permalink)  
Old April 16th, 2008, 11:04 AM
mehere's Avatar
Mistress of Sarcasm

 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 111
Thanks: 8
Thanked 19 Times in 17 Posts
Rep Power: 1
mehere is on a distinguished road

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 Classic ASP 0 March 21st, 2008 12:07 PM


Sponsored Links

ASP.NET Resource Index
a directory of ASP.NET tutorials, applications, scripts, assemblies and articles for the novice to professional developer.

Free Web Directory
Including Chats and Forums Resources, Offer automatic, instant and free directory submissions.
URLZ Web Directory
URLZ Web Directory

Free Web Directory - Add Your Link
The Little Web Directory
Free Web Directory
Pegasus free web directory is a free directory organised by categories.

Web Directory & SEO Services
dirroot web directory


All times are GMT -4. The time now is 07:46 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
Copyright © 2008 DeveloperBarn.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46