Skip to main content
replaced http://dba.stackexchange.com/ with https://dba.stackexchange.com/
Source Link
  • It is always better to present real code in your question - like actual CREATE TABLE statements, a syntactically correct JSON snippet and a query that actually can run.
  • For translating JSON arrays into PostgreSQL arrays, I used here a technique that is described in detail in Erwin Brandstetter's excellent answerexcellent answer.
  • Check the documentation about the different JSON(B) operators and functions. It contains quite a lot of useful stuff.
  • It is always better to present real code in your question - like actual CREATE TABLE statements, a syntactically correct JSON snippet and a query that actually can run.
  • For translating JSON arrays into PostgreSQL arrays, I used here a technique that is described in detail in Erwin Brandstetter's excellent answer.
  • Check the documentation about the different JSON(B) operators and functions. It contains quite a lot of useful stuff.
  • It is always better to present real code in your question - like actual CREATE TABLE statements, a syntactically correct JSON snippet and a query that actually can run.
  • For translating JSON arrays into PostgreSQL arrays, I used here a technique that is described in detail in Erwin Brandstetter's excellent answer.
  • Check the documentation about the different JSON(B) operators and functions. It contains quite a lot of useful stuff.
added 589 characters in body
Source Link
András Váczi
  • 31.6k
  • 13
  • 101
  • 149

Notes:

  • It is always better to present real code in your question - like actual CREATE TABLE statements, a syntactically correct JSON snippet and a query that actually can run.
  • For translating JSON arrays into PostgreSQL arrays, I used here a technique that is described in detail in Erwin Brandstetter's excellent answer.
  • Check the documentation about the different JSON(B) operators and functions. It contains quite a lot of useful stuff.

Notes:

  • It is always better to present real code in your question - like actual CREATE TABLE statements, a syntactically correct JSON snippet and a query that actually can run.
  • For translating JSON arrays into PostgreSQL arrays, I used here a technique that is described in detail in Erwin Brandstetter's excellent answer.
  • Check the documentation about the different JSON(B) operators and functions. It contains quite a lot of useful stuff.
added 279 characters in body
Source Link
András Váczi
  • 31.6k
  • 13
  • 101
  • 149
-- add some more data for testing
INSERT INTO some_data
VALUES ('departement=>bla,access=>bla'),
       ('departement=>it,access=>bla'),
       ('departement=>it,access=>login');

WITH condition_elements (... likeas above ...)
SELECT data,*
 key FROM (
        SELECT data, bool_and(data->key=ANY(value)) AS full_match
          FROM some_data, condition_elements
 WHERE        GROUP BY data->key=ANY(value
       ); t
 WHERE full_match;

       data         key  data          value         full_match 
──────────────────┼────────┼────────────────────────────────────────────────────────────────┼────────────
 "access"=>"user""access"=>"login", "departement"=>"it"accesst
 "access"=>"user"                      {superuser,user,login}t
WITH condition_elements (... like above ...)
SELECT data, key, value 
  FROM some_data, condition_elements
 WHERE data->key=ANY(value);

       data         key            value          
──────────────────┼────────┼────────────────────────
 "access"=>"user"access{superuser,user,login}
-- add some more data for testing
INSERT INTO some_data
VALUES ('departement=>bla,access=>bla'),
       ('departement=>it,access=>bla'),
       ('departement=>it,access=>login');

WITH condition_elements (... as above ...)
SELECT *
  FROM (
        SELECT data, bool_and(data->key=ANY(value)) AS full_match
          FROM some_data, condition_elements
         GROUP BY data
       ) t
 WHERE full_match;

                  data                   full_match 
────────────────────────────────────────┼────────────
 "access"=>"login", "departement"=>"it"t
 "access"=>"user"                      t
added 405 characters in body
Source Link
András Váczi
  • 31.6k
  • 13
  • 101
  • 149
Loading
Source Link
András Váczi
  • 31.6k
  • 13
  • 101
  • 149
Loading