嵌套的postgres查询

时间:2016-03-31 08:13:18

标签: sql postgresql nested-queries

我有两个工作查询,我似乎无法嵌套。

第一个有效:

SELECT * FROM accounts WHERE account = 'some_account';

其次很好用:

SELECT COUNT(*) FROM accounts; 

我想加入这些,以便从第一个查询的结果得到帐户的数量,它看起来像这样,但我不能这样做。

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

我该怎么做?

2 个答案:

答案 0 :(得分:1)

无论

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

或者

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

答案 1 :(得分:1)

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