PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

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 April 11th, 2003, 08:43 AM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question top n sql

I am trying to write a select statement using Postgres to get the top 3 users in each problem category with the most problem incidents.

For example:
email problems:
mary 5
jason 3
amy 2
beth 1
riley 1

network problems:
bob 7
john 6
fred 4
howard 2
amy 1
sue 1

The results would produce:
email problems:
mary 5
jason 3
amy 2

network problems:
bob 7
john 6
fred 4

This contains the subselect that works so far to get me the count of problems by category sorted by count descending within each category. Now I need to extract out the top 3 from each category.

SELECT subtable.type,subtable.requestor,subtable.foo
FROM
(SELECT type as type,requestor as requestor,count(*) as foo
FROM incident
group by type,requestor
ORDER BY type,count(*) DESC) subtable
??????????????????????;

Any help would be much appreciated! Laura

Reply With Quote
  #2  
Old April 11th, 2003, 08:59 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,338 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 h 40 m 53 sec
Reputation Power: 1090
i believe postgresql has LIMIT syntax, so
Code:
    select type
         , requestor
         , problems
      from incident X
     where problems in
           ( select problems
               from incident
              where type = X.type
           order by problems desc  limit 3 )
  order by type
         , problems desc

rudy
http://r937.com/

Reply With Quote
  #3  
Old April 22nd, 2003, 12:30 PM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the feedback. I was hoping I would not have to use views since I have several problem categories and would like to have 'top n' be dynamic based on what the user enters, so a stored procedure would work best. I found that our version of Postgres (7.2) doesn't support set manipulation and has other limitations which prevent me to use functions. So I do in fact need to use the views as you suggested. Appreciate your taking the time!

Reply With Quote
  #4  
Old April 22nd, 2003, 01:04 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,338 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 h 40 m 53 sec
Reputation Power: 1090
views? functions? i did not mention those

my solution uses a subquery, that is all

i do not have postresql to test on, but i'm pretty sure it supports subqueries

give my solution a try


rudy

Reply With Quote
  #5  
Old April 22nd, 2003, 03:04 PM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry for the view/function reference. I got some other suggestions from another source.

I took your subquery and with minor changes that were needed I ran the following which gave me an error in that an aggregate cannot be used in a where clause.

select type
, requestor
, count(*)
from incident X
where count(*) in
( select count(*)
from incident
where type = X.type
group by type,requestor
order by count(*) desc limit 3 )
group by type,requestor
order by type
, count(*) desc;

If you have any further insights I would be glad to hear them. Thanks!

Reply With Quote
  #6  
Old April 22nd, 2003, 03:24 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,338 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 h 40 m 53 sec
Reputation Power: 1090
for that type of construction (your initial question made it look like "problems" was an actual column, not an aggregate), you need HAVING instead of WHERE

select type
, requestor
, count(*)
from incident X
group by type, requestor
HAVING count(*) in
( select count(*)
from incident
where type = X.type
group by requestor
order by count(*) desc limit 3 )
order by type
, count(*) desc;

i'm not sure if you can have a correlated subquery in the HAVING clause but it's certainly worth a try, please let me know if it works

rudy

Reply With Quote
  #7  
Old April 22nd, 2003, 03:34 PM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
It worked like a champ! Thanks so much for sticking with me on this!

Reply With Quote
  #8  
Old April 22nd, 2003, 03:39 PM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ooops, one little problem. If there is more than one occurrence of the same count within a type, you will see more then 3 line items for a type, such as

network rachel 6
network bill 4
network sally 4
network dave 4
network mary 3


Any thoughts?

Reply With Quote
  #9  
Old April 22nd, 2003, 03:55 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Click here for more information.
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 18,338 r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level)r937 User rank is General 1st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 1 h 40 m 53 sec
Reputation Power: 1090
my thoughts? yes, that is exactly the right result

otherwise, you are going to have to find some method of deciding which one of bill, sally, and dave should get kicked out

what do they do in the olympics? (french judges aside)

if bill and sally and dave tie for second place, does that mean one of them doesn't get a silver medal and mary doesn't get the bronze?

i'm sorry, but i think ties should be included

those are my thoughts


Reply With Quote
  #10  
Old April 23rd, 2003, 08:02 AM
lauram lauram is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Posts: 13 lauram User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Your point is right on the money......... Sometimes I can just kick myself for my stupidity... Thanks again for all your help and patience!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > top n sql


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

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

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