Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

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, 21: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
__________________
smArti
Reply With Quote
  #2 (permalink)  
Old 12-03-03, 23: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 02:52.
Reply With Quote
  #3 (permalink)  
Old 12-04-03, 06: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.
__________________
smArti
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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On