Alexander Kleinhans
Alexander Kleinhans

Reputation: 6258

Nested postgres query

I have two working queries I can't seem to nest.

First one works:

SELECT * FROM accounts WHERE account = 'some_account';

Second works just fine:

SELECT COUNT(*) FROM accounts; 

I would like to join these so that I get the count of accounts from the result of the first query and it would look something like this, but I can't do it.

SELECT COUNT(account) FROM (SELECT * FROM accounts WHERE account = 'some_account');

How would I do this?

Upvotes: 1

Views: 39

Answers (2)

S M
S M

Reputation: 3233

select count(case when account = 'some_account' then 1 else null end) as Count
FROM accounts

Upvotes: 1

Matt
Matt

Reputation: 15061

Either

SELECT COUNT(account) 
FROM (SELECT account 
      FROM accounts 
      WHERE account = 'some_account');

Or

SELECT COUNT(*) 
FROM accounts 
WHERE account = 'some_account';

Upvotes: 1

Related Questions