1

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

2 Answers 2

2

This is the correct syntax:

SELECT COUNT(id) AS count_id,
       COUNT(DISTINCT id) AS count_d_id
FROM table 
Sign up to request clarification or add additional context in comments.

Comments

1

Your original query aliases the subquery rather than the column. You seem to want:

SELECT COUNT(*) AS count_d_id FROM (SELECT DISTINCT id FROM table) t
 -- column alias --^                           -- subquery alias --^

1 Comment

ok great thanks! That added in the column name properly - how can I include the additional count column?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.