Search code examples
sqlpostgresqlcountsubquerydistinct

PostgreSQL create count, count distinct columns


fairly new to PostgreSQL and trying out a few count queries. I'm looking to count and count distinct all values in a table. Pretty straightforward -

CountD  Count
351     400

With a query like this:

SELECT COUNT(*)
COUNT(id) AS count_id,
COUNT DISTINCT(id) AS count_d_id
FROM table 

I see that I can create a single column this way:

SELECT COUNT(*) FROM (SELECT DISTINCT id FROM table) AS count_d_id

But the title (count_d_id) doesn't come through properly and unsure how can I add an additional column. Guidance appreciated


Solution

  • This is the correct syntax:

    SELECT COUNT(id) AS count_id,
           COUNT(DISTINCT id) AS count_d_id
    FROM table