If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Complicated query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-03-03, 20:06
tumu tumu is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Question Complicated query

I have a table called PizzaToppings. The columns are Topping ID & PizzaID.

ToppingID is a foreign key from Toppings table & PizzaID is a foreign key from Pizza table.

Together they form the composite primary key for PizzaToppigs table.

I need to find out what the most popular topping is.

ie I have to find the ToppingID with the maximum number of occurrences in the PizzaTopping table.

PLEASE HELP!

Thanks
Reply With Quote
  #2 (permalink)  
Old 12-03-03, 22:07
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Hey,

Given the relation

A B
---------- ----------
a b1
a b2
a b3
a b4
b b2

select a, count(*)
from help
group by a
having count(*) >= all (select count(*) from help group by a);

returns

A COUNT(*)
---------- ----------
a 4

I removed the example with 'rownum = 1', as it contained a vital flaw being it only returns row 1, thus if 2 toppings are equal in popularity then only the first one alphabetically would be returned.
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 12-04-03 at 01:52.
Reply With Quote
  #3 (permalink)  
Old 12-04-03, 05:43
tumu tumu is offline
Registered User
 
Join Date: Dec 2003
Posts: 2
Thumbs up Thanks!

Thanks MUCH! Very obliged.

Quote:
Originally posted by r123456
Hey,

Given the relation

A B
---------- ----------
a b1
a b2
a b3
a b4
b b2

select a, count(*)
from help
group by a
having count(*) >= all (select count(*) from help group by a);

returns

A COUNT(*)
---------- ----------
a 4

I removed the example with 'rownum = 1', as it contained a vital flaw being it only returns row 1, thus if 2 toppings are equal in popularity then only the first one alphabetically would be returned.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On