Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 3rd, 2003, 08:04 AM
monfu monfu is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Location: Dublin
Posts: 40 monfu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 31 m 25 sec
Reputation Power: 6
DISTINCT sql Problem

Dear All,

I have a query where I want to select the top 3 portfolio Items, however these must have distinct Category names. Here is the SQL which I came up with, however it is not working:-

SELECT TOP 3 portfolio.*,DISTINCT [category].[CatName] FROM portfolio INNER JOIN category ON [portfolio].[catID]=[category].[catID] ORDER BY CatName ASC, portfolioID DESC"

Can you please help me out.

Thanks

Reply With Quote
  #2  
Old December 3rd, 2003, 10:05 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,653 r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level)r937 User rank is General (90000 - 100000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 6 Days 12 h 42 m 2 sec
Reputation Power: 981
Code:
select top 3 
       portfolio.*
     , [category].[catname] 
  from portfolio
inner
  join category 
    on [portfolio].[catid]=[category].[catid] 
 where portfolioid in
       ( select min(portfolioid)
           from portfolio
         group
             by catid )      
order
    by catname asc
     , portfolioid desc
the subquery gets one portfolio per category (the one with the lowest id)

then the main query gets all those portfolios, thus only one per category

then the TOP keyword ensures you get only three of them

rudy
http://r937.com/

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > DISTINCT sql Problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway