如何使用R查询PostgreSQL中嵌套的JSONB格式数据列?

ruyhziif  于 2022-12-26  发布在  PostgreSQL
关注(0)|答案(1)|浏览(121)

我在jsosnb列的多行中有不同样本的基因表达数据,如下所示:

Sample       Gexp
Sample A    {"data": [{"pval": 0.0154, "Protein": "A0A0B4J2D5", "FoldChange": 1.3534, "MinusLog10p": 0.1334, "Significance": "Non-significant"}, {"pval": 0.0689, "Protein": "A0FGR8", "FoldChange": 2.5448, "MinusLog10p": 1.1615, "Significance": "Significant"}]}
Sample B    {"data": [{"pval": 0.0824, "Protein": "A0A0B4J2D5", "FoldChange": -0.1676, "MinusLog10p": 0.1084, "Significance": "Non-significant"}, {"pval": 0.0219, "Protein": "A0FGR8", "FoldChange": 2.3448, "MinusLog10p": 1.1615, "Significance": "Significant"}]}

我需要查询包含多个记录的列,其中某个蛋白质的pval或FoldChange在某个范围内。我尝试了这个论坛提供的多种解决方案(Search in nested Postgresql JSONB columnPostgresql query for objects in nested JSONB fieldQuery simplified JSONB form JSONB column containing nested JSON from a Postgresql database?How to query nested array with heterogeneous elements in PostgreSQL JSONB column等),但没有成功。有人能帮助我吗?

a64a0gku

a64a0gku1#

问题中没有精确地(明确地)描述数据选择的条件,例如,当我们在0.020.03pval范围内寻找A0FGR8蛋白质时,查询可能如下所示:

select sample, value
from my_table
cross join jsonb_array_elements(gexp->'data')
where value->>'Protein' = 'A0FGR8'
and (value->>'pval')::numeric between 0.02 and 0.03

Db<>fiddle.中测试查询

相关问题