General SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesGeneral SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 11th, 2006, 01:45 PM
doog7 doog7 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Posts: 1 doog7 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 32 m 11 sec
Reputation Power: 0
"count" query

i need to query the following tables to return a category name,category description, username of user who made category and no. of posts in each category.i am using this for a blog i am developing which needs to display a list of categories including category description, user who posted the category and no. of posts in the category
i have been using the following query but doesnt seem to work for me

select C.CATEGORY_NAME,C.DESCRIPTION,U.USER_ID,count(P.PO ST_ID)
from CATEGORY C,REGISTERED_USER U,POST P
where P.CATEGORY_ID = C.CATEGORY_ID;

the error message i am getting is
ERROR at line 1:
ORA-00937: not a single-group group function

SQL> desc REGISTERED_USER;
Name Null? Type
----------------------------------------- -------- ----------------------------
USER_ID NOT NULL NUMBER
USERNAME NOT NULL VARCHAR2(10)
PASSWORD NOT NULL VARCHAR2(10)

SQL> desc CATEGORY;
Name Null? Type
----------------------------------------- -------- ----------------------------
CATEGORY_ID NOT NULL NUMBER
CATEGORY_NAME NOT NULL VARCHAR2(255)
USER_ID NUMBER
DESCRIPTION VARCHAR2(255)

SQL> desc POST;
Name Null? Type
----------------------------------------- -------- ----------------------------
POST_ID NOT NULL NUMBER
CATEGORY_ID NUMBER
USER_ID NUMBER
POST_TITLE NOT NULL VARCHAR2(255)
DATE_CREATED NOT NULL DATE
POST_BODY_TEXT NOT NULL VARCHAR2(255)

i used the following commands to create the above tables

create table REGISTERED_USER
(
USER_ID number primary key,
USERNAME varchar2(10) not null,
PASSWORD varchar2(10) not null
);

create table CATEGORY
(
CATEGORY_ID number primary key,
CATEGORY_NAME varchar2(255) not null,
DESCRIPTION varchar2(255),
USER_ID references REGISTERED_USER
);

create table POST
(
POST_ID number primary key,
CATEGORY_ID references CATEGORY,
USER_ID references REGISTERED_USER,
POST_TITLE varchar2(255) not null,
DATE_CREATED date not null,
POST_BODY_TEXT varchar2(255) not null
);

thanks

Reply With Quote
  #2  
Old January 12th, 2006, 07:35 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
I forget the formal explanation, but you need to have a GROUP BY clause if you're using certain methods while joining multiple tables.

Try the following:
Code:
select C.CATEGORY_NAME,C.DESCRIPTION,U.USER_ID,count(P.PO   ST_ID)
from CATEGORY C,REGISTERED_USER U,POST P
where P.CATEGORY_ID = C.CATEGORY_ID
group by C.CATEGORY;
Comments on this post
Laban agrees: Yes, GROUP BY is required when selecting from more than one column. You can get away with the query
without GROUP BY if it was only one.
__________________
Daryl's Homepage | My Blogroll | My Profile | Firefox supporter!
DevArticles Forum Moderator

"The net is a waste of time, and that's exactly what's right about it." -- William Gibson

Reply With Quote
  #3  
Old February 9th, 2006, 04:51 AM
Laban Laban is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2005
Posts: 40 Laban User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 15 m 32 sec
Reputation Power: 4
lol.. did I just "Agree" to a MadCowzz explanation? Wonder if Einstein would be impressed if a n00b told him "Yes, your theory is correct but only if..."
Comments on this post
MadCowDzz agrees!

Reply With Quote
  #4  
Old February 9th, 2006, 07:49 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 14 m 9 sec
Reputation Power: 8
Ya, uh, I left taht one vague on purpose... at least that's my story and I'm sticking to it!
Thanks for the followup Laban

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesGeneral SQL Development > "count" query


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 1 hosted by Hostway