/ / Come selezionare i valori in var usando l'operazione BIT? - postgresql, funzione

Come selezionare i valori in var usando l'operazione BIT? - postgresql, funzione

Ho un tavolo

create table t
(id serial primary key,
name text,
val INTEGER);


insert into t(name, val)
select "user1", x"0001"::INT
union all
select "user1", x"0010"::INT
union all
select "user1", x"0110"::INT
union all
select "user2", x"0001"::INT

Come posso selezionare i valori in variabili usando l'operazione bit per il nome dato?

create or replace function get_union(
name text,
OUT retval int
)
as $BODY$
begin
-- ?
end
$BODY$ language plpgsql;

Ad esempio, la funzione dovrebbe restituire 111 per il nome "utente1"

select to_hex(x"0001"::int | x"0010"::int | x"0100"::int);
---
111

risposte:

1 per risposta № 1
select to_hex(bit_or(val)), bit_or(val), bit_or(val)::bit(16)
from t
where name = "user1";
to_hex | bit_or |      bit_or
--------+--------+------------------
111    |    273 | 0000000100010001