I have a problem which I can’t really see how to work, I’ve tried some things based on findings on
Google, but to no avail. I hope someone here can help.
I have two tables (A and B), in table A I have some overview data which I want to select, and in table B I have details corresponding to table A (joined on two parameters), however the data in table B only needs to be summarized on number of rows.
I have this query:
SELECT s.CartID,
s.Sender,
s.Destination,
s.CartType,
s.SendDate,
p.PackageID,
p.CartID,
COUNT(b.*) AS nRows
FROM tblA s LEFT OUTER JOIN tblB p
ON s.CartID = p.CartID AND s.SendDate = p.CartDate
WHERE s.Client='3' AND s.SendDate BETWEEN '2012-09-01' AND '2012-09-07'
However, this only gives the following errror:
Incorrect syntax near ‘‘.*
I’ve also tried using this COUNT(b.PackageID) instead but then I get:
Column ‘tblA.CartID’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
I really don’t see how I’m going to get this data in one query.
Any help would be appreciated. 🙂
The error is telling you that you need to add all fields in the
SELECTto either aGROUP BYor an aggregate like this:If you do not want to
GROUP BYall of those fields, then you can also use a sub-query similar to this: