Firebird SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesFirebird SQL 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 September 5th, 2004, 06:02 PM
russh russh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 18 russh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 48 sec
Reputation Power: 0
Question Help with query - Invalid expression in the select list

Hello,

As an excercise I am trying to port a simple mysql book-library app to firebird (1.5). I am having trouble with the following query. It should return a list of book categories and how many books are in each category.

There is a 'category' table which includes info such as the name of the category etc. There is also a books_to_categories table which simply cross references books to categories (categoryID | bookID).

In mysql, the following query gets a list of category names and ids, as well as how many books are in each category. It works by COUNTing the number of rows in the cross-reference table that contain each category id (and since each matching row is a link to a book, gives the total number of books in this category). However, in firebird, I get the following error:

"Invalid expression in the select list (not contained in either an aggregate function or the GROUP BY clause)."

Could anyone give me a clue as to what I'm doing wrong?

SELECT
c.categoryid,
c.name,
COUNT(x.categoryid)
FROM
categories c
LEFT JOIN
books_to_categories x
ON c.categoryid = x.categoryid
GROUP BY c.categoryid


Any pointers/advice would be very much appreciated. Been wrestling with this one for a while!

Thanks,
Russ

Reply With Quote
  #2  
Old September 6th, 2004, 12:05 AM
pmanten pmanten is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 51 pmanten User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally Posted by russh
SELECT
c.categoryid,
c.name,
COUNT(x.categoryid)
FROM
categories c
LEFT JOIN
books_to_categories x
ON c.categoryid = x.categoryid
GROUP BY c.categoryid




Try the group by clause:
GROUP BY c.categoryid, c.name

If you combine in the select an aggregate function (count, sum, max) together with 'normal' colums (id, name, etc.), all the normal columns have to be in the group by clause.

Peter

Reply With Quote
  #3  
Old September 6th, 2004, 04:54 AM
russh russh is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 18 russh User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 48 sec
Reputation Power: 0
Thanks Peter. Seems a little obvious now looking back at the error message! Oh well, hindsight and all that. Cheers.

Reply With Quote
  #4  
Old September 6th, 2004, 05:20 AM
pmanten pmanten is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 51 pmanten User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Quote:
Originally Posted by russh
Seems a little obvious now looking back at the error message!
Happens to all of us now and then!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesFirebird SQL Development > Help with query - Invalid expression in the select list


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