我有一张名为大师的邮寄桌子.

ID Reference CustomFieldsJson
1 John [ { "ID": 17,"NumericValue": 1234},{ "ID":21,"DateValue":"2023-06-14T00:00:00Z"},{ "ID": 24, "BooleanValue": false }
2 Smith [ { "ID": 17,"NumericValue": 1234},{ "ID":12,"DateValue":"2023-04-05T00:00:00Z"},{ "ID": 15, "ListItemValue": "india" }

CustomFieldsJson列数据类型为jsonb. 我想通过custom_fields列键Date Value过滤行.为此,我使用了Postgres Documentation中描述的json路径表达

select id,reference,custom_fields_json from master.masters
where custom_fields_json @? '$[*] ? (@.DateValue.datetime() > "2022-06-22T03:09:37Z".datetime())'

但过滤不起作用.查询不返回任何行.我认为日期格式存在问题.在文档中,他们显示了日期格式为"2015-08-12"的示例

推荐答案

该问题与日期格式有关.您的SON数据中的日期格式为ISO 8601格式,该格式应该与PostgreSQL的SON函数良好配合

请try 这个

SELECT id, reference, custom_fields_json 
FROM master.masters
WHERE custom_fields_json @? '$[*] ? (@.DateValue > "2022-06-22T03:09:37Z")';

示例:

大师赛桌:

id reference customfields
1 John [{"ID": 17, "NumericValue": 1234}, {"ID": 21, "DateValue": "2023-06-14T00:00:00Z"}, {"ID": 24, "BooleanValue": false}]
2 Smith [{"ID": 17, "NumericValue": 1234}, {"ID": 12, "DateValue": "2023-04-05T00:00:00Z"}, {"ID": 15, "ListItemValue": "india"}]

查询 Select Date Value大于"2023-05- 22 T03:09:37 Z"的行

SELECT id, reference, CustomFields 
FROM masters
WHERE CustomFields @? '$[*] ? (@.DateValue > "2023-05-22T03:09:37Z")';

输出:

id reference customfields
1 John [{"ID": 17, "NumericValue": 1234}, {"ID": 21, "DateValue": "2023-06-14T00:00:00Z"}, {"ID": 24, "BooleanValue": false}]

fiddle

Sql相关问答推荐

识别顺序记录组

如何查询一个名称是根据PL/pgSQL函数结果构建的表?

使用SQL旋转表的列(Snowflake)

用于动态查询情况的存储过程常识模式

使用WHERE子句进行筛选时,SQL SELECT查询返回总计数

SQL Select 最小优先级

PostgreSQL抽奖查询

我怎样才能得到列值对应的最大值在另一个?

ColdFusion+Docker:未安装SQLSERVER包

多条件SQL排序行为

在Postgres,什么是;.USSTZ;在';YYYY-MM-DD;T;HH24:MI:SS.USSTZ';?

在 PostgreSQL 中生成时间序列查询

不同计数的 Postgres PIVOT 表

使用 SQL 将列添加到 Access 数据库时出错

使用 XML 作为 SQL 表

AdventureWorks 查询

如何为给定的股票数据集计算利润/亏损,确保先卖出先买入的股票

创建定时器以更新Gridview

所有列分组的简写?

如何根据另一列对行值进行分组?