Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

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 June 20th, 2005, 10:02 AM
pbm1978 pbm1978 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: UK
Posts: 5 pbm1978 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 4 sec
Reputation Power: 0
Select Count problem involving 2 tables

Hi folks

I have the following two table set up:

CATEGORIES
-------------
CATID (primary key)
CATNAME

NEWS
---------
CATID (foreign key matched to CATID above)
NEWSID (foreign key from another table)

What I am trying to do is select each category name from the first table and then show the number of associated news stories (Count(NEWSID)) from the second table. I am beginning to think this cannot be done using just one SQL statement because multiple rows are involved. Can anyone help please?

Reply With Quote
  #2  
Old June 20th, 2005, 10:21 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,925 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 9 m 23 sec
Reputation Power: 279
On Oracle 9 or above you can use something like:
Code:
SELECT c.catname, count(n.newsid) news_number FROM categories c INNER JOIN news n ON c.catid = n.catid;

Reply With Quote
  #3  
Old June 20th, 2005, 10:47 AM
pbm1978 pbm1978 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: UK
Posts: 5 pbm1978 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 4 sec
Reputation Power: 0
Hmmm, that's a nice idea and is exactly what I'm looking for. Unfortunately it results in a ORA-00937 error when I try it. I'm a bit of a novice at writing SQL queries so would welcome any pointers. Database is Oracle 9.2

Reply With Quote
  #4  
Old June 20th, 2005, 10:58 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,925 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 3 Weeks 3 Days 20 h 9 m 23 sec
Reputation Power: 279
Dont' worry, just a stupid mistake by me, but the error message should have told you everything ....

SELECT c.catname, count(n.newsid) news_number
FROM categories c INNER JOIN news n ON c.catid = n.catid
GROUP BY c.catname;

Reply With Quote
  #5  
Old June 20th, 2005, 11:09 AM
pbm1978 pbm1978 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Location: UK
Posts: 5 pbm1978 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 49 m 4 sec
Reputation Power: 0
pabloj, you're a star. Many thanks!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Select Count problem involving 2 tables


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 2 hosted by Hostway
Stay green...Green IT