Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6796311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:27:07+00:00 2026-05-26T18:27:07+00:00

I have following query in PostgreSQL: SELECT COUNT(a.log_id) AS overall_count FROM Log as a,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:27:08+00:00Added an answer on May 26, 2026 at 6:27 pm
    SELECT a.license_id, a.limit_call
         , count(b.license_id) AS overall_count
    FROM   "License"  a
    LEFT   JOIN "Log" b USING (license_id)
    WHERE  a.license_id = 7 
    GROUP  BY a.license_id  -- , a.limit_call  -- add in old versions
    HAVING a.limit_call > count(b.license_id)
    

    Since Postgres 9.1 the primary key covers all columns of a table in the GROUP BY clause. In older versions you’d have to add a.limit_call to the GROUP BY list. The release notes for 9.1:

    Allow non-GROUP BY columns in the query target list when the primary
    key is specified in the GROUP BY clause

    Further reading:

    • Why can't I exclude dependent columns from `GROUP BY` when I aggregate by a key?

    The condition you had in the WHERE clause has to move to the HAVING clause since it refers to the result of an aggregate function (after WHERE has been applied). And you cannot refer to output columns (column aliases) in the HAVING clause, where you can only reference input columns. So you have to repeat the expression. The manual:

    An output column’s name can be used to refer to the column’s value in
    ORDER BY and GROUP BY clauses, but not in the WHERE or HAVING
    clauses; there you must write out the expression instead.

    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 of JOIN, so you do not exclude licenses without any logs at all.

    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 use count(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 of 1 for 0 rows in the left table, use that.

    Aside: I would advise not to use mixed case identifiers in Postgres if possible. Very error prone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query: select column_name, count(column_name) from table group by column_name having
I have the following query: SELECT created_at::DATE, count (*) FROM messages WHERE city =
I have the following query: SELECT c.* FROM companies AS c JOIN users AS
I have the following query: SELECT rowid FROM table1 ORDER BY RANDOM() LIMIT 1
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have a query that looks like the following: SELECT * FROM table WHERE
I have the a postgre sql query as follows SELECT cNo,max(numLogs),name,surname FROM details GROUP
I have the following query: SELECT table_a.field1, table_b.field1 FROM table_a, table_c LEFT JOIN table_b
The following PostgreSQL text search select ID, DISPLAY_NAME, ts_rank_cd(to_tsvector('english', display_name), query) as RANK from
I have a query like the following: Select new mypackage.MyClass( u, max(sc.serviceDate)) from Unit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.