嵌套的 postgres 查询
Nested postgres query
我有两个工作查询似乎无法嵌套。
第一个有效:
SELECT * FROM accounts WHERE account = 'some_account';
第二个工作正常:
SELECT COUNT(*) FROM accounts;
我想加入这些,以便从第一个查询的结果中获取帐户数,它看起来像这样,但我做不到。
SELECT COUNT(account) FROM (SELECT * FROM accounts WHERE account = 'some_account');
我该怎么做?
两者都
SELECT COUNT(account)
FROM (SELECT account
FROM accounts
WHERE account = 'some_account');
或者
SELECT COUNT(*)
FROM accounts
WHERE account = 'some_account';
select count(case when account = 'some_account' then 1 else null end) as Count
FROM accounts
我有两个工作查询似乎无法嵌套。
第一个有效:
SELECT * FROM accounts WHERE account = 'some_account';
第二个工作正常:
SELECT COUNT(*) FROM accounts;
我想加入这些,以便从第一个查询的结果中获取帐户数,它看起来像这样,但我做不到。
SELECT COUNT(account) FROM (SELECT * FROM accounts WHERE account = 'some_account');
我该怎么做?
两者都
SELECT COUNT(account)
FROM (SELECT account
FROM accounts
WHERE account = 'some_account');
或者
SELECT COUNT(*)
FROM accounts
WHERE account = 'some_account';
select count(case when account = 'some_account' then 1 else null end) as Count
FROM accounts