Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL - How to get the count of elements in a column list

This is how my orders table looks like :

-----------------------------------------------------------
| id  | order
-----------------------------------------------------------
|1    |[{"order_quantity" : 2, "active" : TRUE, "price" : $100 }, {"order_quantity" : 4, "active" : FALSE, "price" : $200 }]
|2    |[{"order_quantity" : 2, "active" : TRUE, "price" : $170 }]
|3    |[{"order_quantity" : 2, "active" : TRUE, "price" : $120 }]
|4    |[{"order_quantity" : 2, "active" : TRUE, "price" : $150 }, {"order_quantity" : 3, "active" : TRUE, "price" : $200 }, {"order_quantity" : 5, "active" : TRUE, "price" : $200 }]
-----------------------------------------------------------

the results wanted when doing the count for the JSON elements inside the brackets WHERE active == TRUE in each element :

------------
id  | counts
------------
|1  |   1
|2  |   1
|3  |   1
|4  |   3
------------

This is what I'm using but it doesn't give the data i'm looking for because it doesn't not look into each dictionary to see if active == TRUE

SELECT id, json_array_length(order::JSON)
FROM orders

------------
id  | counts
------------
|1  |   2
|2  |   1
|3  |   1
|4  |   3
------------
like image 257
mongotop Avatar asked Jul 21 '16 18:07

mongotop


People also ask

How do I count values in a column in PostgreSQL?

The PostgreSQL COUNT function counts a number of rows or non-NULL values against a specific column from a table. When an asterisk(*) is used with count function the total number of rows returns. The asterisk(*) indicates all the rows.

How do I count items in PostgreSQL?

The COUNT(*) function returns the number of rows returned by a SELECT statement, including NULL and duplicates. When you apply the COUNT(*) function to the entire table, PostgreSQL has to scan the whole table sequentially. If you use the COUNT(*) function on a big table, the query will be slow.

How do I create a count query in PostgreSQL?

PostgreSQL COUNT SELECTSELECT COUNT ( [*], [DISTINCT] [column_name] ) FROM TABLE_NAME; Let's dig a little deeper into the syntax shown above: SELECT – This is used to select certain columns from the database. COUNT – This is used to count the number of records in this table.

How do I count characters in PostgreSQL?

CHAR_LENGTH() function The PostgreSQL char_length function or character_length function is used to count the number of characters in a specified string. The CHARACTER_LENGTH() function is similar to CHAR_LENGTH() function. A string whose length is to be retrieved.


1 Answers

Use json_array_elements() which selects all elements of the json array, filter the elements and finally count remaining elements grouping by id.

select id, count(id)
from orders
cross join json_array_elements(orders) elem
where (elem->>'active')::boolean
group by 1
order by 1;

Live demo in Db<>fiddle.

Notes:

  • use set returning functions (like json_array_elements()) in FROM clause as lateral join;
  • json boolean values should look like true (not TRUE);
  • there is no money type in json, use 300 instead of $300;
  • use jsonlint to verify json values.
like image 129
klin Avatar answered Sep 18 '22 12:09

klin