У меня есть таблица postgres с полем требований (поле json). Мне нужно посчитать ненулевые ключи в json. Какой будет лучший подход? В настоящее время я запрашиваю это так:
select COALESCE(t_count,0) + COALESCE(p_count,0) as total_count from (select
CASE
WHEN requirements->'FRIDGE_SHELF_SPACE'->'order_frequency' is not null then 1
ELSE 0
END as t_count,
CASE
WHEN requirements->'SUPPLY_CHAIN'->'supply_chain_need' is not null then 1
ELSE 0
END as p_count
from business_requirements where user_id=3561) as t
Fyi весь json (в поле требований) выглядит примерно так
{
"FRIDGE_SHELF_SPACE": {
"order_frequency": {
"question": "How frequently will you be reordering stock?",
"answer": "Weekly reordering"
},
"units_per_order": {
"question": "What is your estimated number of units per order?",
"answer": "10 - 20"
}
},
"Rational_TAP_LINES": {
},
"PERMANENT_TAP_LINES": {
},
"SUPPLY_CHAIN": {
"supply_chain_need": {
"question": "What is your supply chain need?",
"answer": "Brewing ingredients"
},
"supply_chain_requirements": {
"question": "Select any of the following requirements (leave blank if not applicable)",
"answer": [
"Malt",
"Yeast"
]
}
},
"RESEARCH": {
}
}