Sql 计数非空json键->;postgres表中的值

Sql 计数非空json键->;postgres表中的值,sql,laravel,postgresql,Sql,Laravel,Postgresql,我有一个带有字段需求(json字段)的postgres表。我需要计算json中的NOTNULL键。最好的方法是什么?目前我是这样质疑的 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

我有一个带有字段需求(json字段)的postgres表。我需要计算json中的NOTNULL键。最好的方法是什么?目前我是这样质疑的

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
{
  "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": {

  }
}
仅供参考,整个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
{
  "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": {

  }
}

您可以使用postgres
json\u each
函数来计算键数:


select count(key) from json_each('{
  "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": {

  }
}') where exists (select 1 from json_each(value) s);
希望这对您有所帮助:)
祝你好运

谢谢贾斯平德。这真的很有帮助。