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

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

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 December 10th, 2012, 07:02 PM
roynl roynl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 roynl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 25 sec
Reputation Power: 0
Help on finding groups with a similar count as one groupmember on a certain column

I have two tables:

1: A table Person (P) which contains (among others) the column
number

2: A table Ticket (T) which contains (among others) the columns number and penalty.

I need to make a query to find all persons with the same amount of tickets as a certain person. In other words, if the person with number x has y penalties, I need to find all other persons with y penalties.

I already formulated the query beneath:

SELECT P.number, COUNT(T.penalty) number_of_tickets
FROM Person D, Ticket T
WHERE T.number = P.number
GROUP BY P.number

I guess I need to use 'HAVING' but am not sure how to solve my query.

Anyone can help a newbie?

Reply With Quote
  #2  
Old December 20th, 2012, 04:59 AM
roynl roynl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 roynl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 25 sec
Reputation Power: 0
Maybe a screenshot will make the question more clear (screenshot did not happen, do not know/ am not able to post it here. transcripted it in text...):

------------------------------------------------------------------------
SELECT B.lidnummer, COUNT(B.betalingnummer) aantal_boetes
FROM Boete B
GROUP BY B.lidnummer

DATA OUTPUT:

lidnummer aantal_boetes
130 1
112 1
109 2
201 7
208 9
------------------------------------------------------------------------
My question is how I can extract the data with the same 'aantal_boetes' as 'lidnummer' 112.

So 'lidnummer 112' has 'aantal_boetes 1'
'lidnummer 130' also has 'aantal_boetes 1'

So how can I extract those (and in such a way that if 'lidnumer 112' had a different count of 'aantal_boetes' it would automatically adjust).

How would I change my query to get that info?

Reply With Quote
  #3  
Old December 20th, 2012, 09:10 AM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 2,349 swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level)swampBoogie User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 7 h 17 m 46 sec
Reputation Power: 390
Code:
select lidnummer,
       count(*) aantal_boetes
  from Boete
 group
    by lidnummer
having count(*) =
       (select count(*)
          from Boete
         where lidnummer = 112)
   and lidnummer <> 112
Comments on this post
roynl agrees!

Reply With Quote
  #4  
Old December 20th, 2012, 10:13 AM
roynl roynl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2012
Posts: 3 roynl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 36 m 25 sec
Reputation Power: 0
Thank you very much.

Just starting to learn making queries and did not yet get to the concept of using a query into a query (as in a second SELECT, FROM, WHERE part).

Thanks again!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Help on finding groups with a similar count as one groupmember on a certain column

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap