sql - 按位运算符 Postgres

标签 sql postgresql bitwise-operators

我有一个包含二进制值的表,例如:

binaryid    description
1           description1
2           description2
4           description3
8           description4

我还有另一个包含值的表:

DBID BinaryTogether
1    15
1    12
1     6

使用按位运算符,我想得到一个包含以下内容的表:

DBID   BinaryTogether BitwiseResult
1      15             description1,description2,description3,description4
1      12             description3,description4
1       6             description2, description3

最佳答案

您可以使用 & 按位运算符连接这些表,然后使用 string_agg 函数聚合描述。这是示例:

with 
  b(x,d) as (
    values
      (1,'description1'),
      (2,'description2'),
      (4,'description3'),
      (8,'description4')),
  p(y) as (
    values
      (15),
      (12),
      (6))
select
  y,
  string_agg(d,',')
from
  p join b on (x & y != 0)
group by
  y

关于sql - 按位运算符 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37578015/

相关文章:

sql - 如何拆分出现两次的字符串?

php - 求和并显示总分

c# - 无法确定类型为 'Npgsql.NpgsqlFactory' 的提供程序工厂的提供程序名称

postgresql - 连接到通过ubuntu服务器上的docker运行的postgres实例

python - 继承int的类中的位运算

python - 获取 1 位在 python Long 对象中的位置

java - 为什么我们通常使用||结束|?有什么区别?

java - 使用 JDBC 将 Sql 查询结果集导出到逗号分隔的文件

sql - 修复 Amazon Redshift 上计算 DAU 和 MAU 时的 MAU 问题

有 2 个条件的 SQL 连接