I have two table in my data base As below:
CUSTOMER (
ID integer Generated Always As Identity,
FIRSTNAME varchar(32) not null,
LASTNAME varchar(32) not null,
PHONENUMBER varchar(11) not null,
ADDRESS varchar(225),
primary key (id)
);
and
APP.ORDERS (
ID integer not null default AUTOINCREMENT: start 1 increment 1,
CUSTOMER_ID integer References Customer(ID) ,
EMPLOYEEID integer References Employee(ID),
DATEOFDELIVERY date,
primary key (id),
);
When I try To join This two table as below every thing is fine:
SELECT CUSTOMER_ID,firstname,LASTNAME,Address,phoneNumber From APP.Customer
INNER JOIN APP.Orders ON customer.ID = customer_ID
WHERE ORDERS.dateOfDelivery Between '2012-09-01' AND '2012-11-11'
but When I use Aggregate Function as below it give me Error:
SELECT Count(CUSTOMER_ID),firstname,LASTNAME,Address,phoneNumber
From APP.Customer
INNER JOIN APP.Orders ON customer.ID = customer_ID
WHERE ORDERS.dateOfDelivery Between '2012-09-01' AND '2012-11-11' ;
Error:
[2012-07-20 08:07:04] [42Y35][30000] Column reference 'FIRSTNAME' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions.
I try to change the last code above to Orders.Count(CUSTOMR_ID) to tell the database the correct table but it give me error that there is no schema with name Orders so I change it to APP.Orders.Count(CUSTOMER_ID) but it never solve the problem.
Error :Schema 'ORDERS' does not exist
UPDATING QUESTION:
I Figur out that very simple Query Like :
SELECT COUNT(*),ID FROM Orders
will caused to This Error :
[2012-07-20 09:01:50] [42Y35][30000] Column reference 'ID' is invalid. When the SELECT list contains at least one aggregate then all entries must be valid aggregate expressions.
So It means that it is not possible to have both aggregate function and simple column names together but I am wonder how to supply the expected out put with any equivalent Query?Any Idea?
GROUP BY is the correct approach. You need to group by all the non-aggregate columns, as in:
Think about it this way: you want to gather together all the orders for a particular customer (that’s what GROUP BY does), then you want to count up all those orders (that’s what the COUNT() function does).