postgresqlpostgresql-9.5

PostgreSQL: get count of occurrences of array1 values in array2


I have a table:

client_id order_id items_current_order items_next_order
905562 4254677 {1, 2, 4} {1}
905562 4254645 {1, 2, 5, 6} {1, 6}

I need to count the proportion of items from the items_next_order contained in the items_current_order.

For example, for order_id = 4254645, the share should be 50% (or 0.5), since only 2 of the 4 items_next_order items are contained in items_current_order.

I tried to calculate the amount (not the share) like this::

SELECT
    client_id ,
    COUNT(1) AS count_values
FROM
    (SELECT
         client_id ,
         unnest(items_next_order) AS value
     FROM
         orders
     GROUP BY
         1, 2) AS a
WHERE
    value = ANY (SELECT unnest(items_current_order) FROM orders as rj WHERE rj.client_id = a.client_id )
GROUP BY
    client_id 

but I need to count in the grouping by client_id , order_id

The result that I expect:

client_id order_id items_current_order items_next_order share
905562 4254677 {1, 2, 4} {1} 0.33
905562 4254645 {1, 2, 5, 6} {1, 6} 0.5

Solution

  • Here's a working query which accomplishes what you are looking to do:

    SELECT 
     order_id, 
     items_current_order,
     items_next_order,
     (
        SELECT COUNT(*) FROM unnest(items_current_order) AS i
       WHERE i = ANY(items_next_order)
     )::FLOAT / array_length(items_current_order,1)::FLOAT AS share
     FROM T
    

    Explanation:

    You can see a working SQL Fiddle with this query here: https://www.db-fiddle.com/f/5opYDXhZu5zvK8An8YyWRs/0