gpt4 book ai didi

postgresql - Postgres 位掩码中 "1s"的总数

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

有没有办法获取 Postgres“位串”类型中 1 的总数?

最佳答案

# select length(replace(x::text, '0', '')) from ( values ('1010111101'::bit varying) ) as something(x);
length
--------
7
(1 row)

无需字符串转换的方法:

# select count(*) from ( select x, generate_series(1, length(x)) as i from ( values ('1010111101'::bit varying) ) as something(x) ) as q where substring(x, i, 1) = B'1';
count
-------
7
(1 row)

关于postgresql - Postgres 位掩码中 "1s"的总数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1910369/

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