gpt4 book ai didi

sql - 在 POSTGRES 中每行返回一个计数而不是总和或单行

转载 作者:行者123 更新时间:2023-11-29 12:34:16 25 4
gpt4 key购买 nike

我对此使用了变体:

SELECT (select count(active) AS true 
from sooper_entry
where active = 't'
and entry_id_ref = 28) AS true,
(select count(active) AS false
from sooper_entry
where active = 'f'
and entry_id_ref = 28) AS false;

所以我可以获得所有真假计数,但我需要在关联数组中返回真假计数。

期望的结果:

 true | false | uId 
------+-------+-----
16 | 0 | 1
10 | 2 | 3
13 | 10 | 4
19 | 8 | 5
12 | 3 | 8
21 | 0 | 12
(6 rows)

最佳答案

SELECT 
sum(case when active = 't' then 1 else 0 end) AS true,
sum(case when active = 'f' then 1 else 0 end) AS false,
entry_id_ref
FROM sooper_entry
GROUP BY entry_id_ref

关于sql - 在 POSTGRES 中每行返回一个计数而不是总和或单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6818265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com