I have following query in PostgreSQL:
SELECT
COUNT(a.log_id) AS overall_count
FROM
"Log" as a,
"License" as b
WHERE
a.license_id=7
AND
a.license_id=b.license_id
AND
b.limit_call > overall_count
GROUP BY
a.license_id;
Why do I get this error:
ERROR: column "overall_count" does not exist
My table structure:
License(license_id, license_name, limit_call, create_date, expire_date)
Log(log_id, license_id, log, call_date)
I want to check if a license has reached the limit for calls in a specific month.
Major points
In versions prior to PostgreSQL 9.1 you have to add
limit_call
to theGROUP BY
clause. Beginning with version 9.1 it is enough to have the primary key in theGROUP BY
clause. The release notes for 9.1:Your
WHERE
condition has to move to theHAVING
clause since it refers to the result of an aggregate function. And you cannot refer to output columns (column aliases) in theHAVING
clause, where you can only reference input columns. So you have to repeat the expression. Per documentation:I reversed the order of tables in the
FROM
clause and cleaned up the syntax a bit to make it less confusing.USING
is just a notational convenience here.I used
LEFT JOIN
instead ofJOIN
, so you do not exclude licenses without any logs at all.I would advise not to use mixed case identifiers in Postgres if possible. Very error prone.
Only non-null values are counted by
count()
. Since you want to count related entries in table"Log"
it is safer and slightly cheaper to usecount(b.license_id)
. This column is used in the join, so we don't have to bother whether the column can be null or not.count(*)
is even shorter and slightly faster, yet. If you don't mind to get a count of1
for0
rows in the left table, use that.The
where
query doesn't recognize your column alias, and furthermore, you're trying to filter out rows after aggregation. Try:The
having
clause is similar to thewhere
clause, except that it deals with columns after an aggregation, whereas thewhere
clause works on columns before an aggregation.Also, is there a reason why your table names are enclosed in double quotes?