Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL:从字典文本列中提取最近的时间戳_Sql_Json_Postgresql_Select_Jsonb - Fatal编程技术网

SQL:从字典文本列中提取最近的时间戳

SQL:从字典文本列中提取最近的时间戳,sql,json,postgresql,select,jsonb,Sql,Json,Postgresql,Select,Jsonb,给定一个只有一列的表,其中每一行都是Python程序生成的时间戳dict的字符串表示,是否有一种仅使用SQL来生成最近的时间戳及其关联值的方法 示例行: {“2018-08-12T00:00:00”:888.888,“2018-08-12T15:00:00”:88888,“2018-08-12T03:00:00”:88888,“2018-08-12T12:00:00”:88888,“2018-08-12T21:00:00”:888,“2018-08-12T18:00:00”:88888,“201

给定一个只有一列的表,其中每一行都是Python程序生成的时间戳dict的字符串表示,是否有一种仅使用SQL来生成最近的时间戳及其关联值的方法

示例行:
{“2018-08-12T00:00:00”:888.888,“2018-08-12T15:00:00”:88888,“2018-08-12T03:00:00”:88888,“2018-08-12T12:00:00”:88888,“2018-08-12T21:00:00”:888,“2018-08-12T18:00:00”:88888,“2018-08-12T06:00:00”:888,“2018-08-08-12T09:00:00:00”:888
如果CSV:
如示例数据所示,dict值是有效的json字符串。我建议将它们转换为jsonb对象;然后,您可以使用
jsonb\u object\u keys()
提取键,对它们进行排序,并检索对应于最大键的值:

select z.ts, (t.dict::jsonb) ->> z.ts val
from t
cross join lateral (
    select ts
    from jsonb_object_keys(t.dict::jsonb) x(ts)
    order by ts desc
    limit 1
) z

with t as (select '{"2018-08-12T00:00:00": 88888.8, "2018-08-12T15:00:00": 88888.8, "2018-08-12T03:00:00": 88888.8, "2018-08-12T12:00:00": 88888.8, "2018-08-12T21:00:00": 88888.8, "2018-08-12T18:00:00": 88888.8, "2018-08-12T06:00:00": 88888.8, "2018-08-12T09:00:00": 88888.8}' dict)
select z.ts, (t.dict::jsonb) ->> z.ts val
from t
cross join lateral (
    select ts
    from jsonb_object_keys(t.dict::jsonb) x(ts)
    order by ts desc
    limit 1
) z
塔斯瓦尔 :------------------ | :------ 2018-08-12T21:00:00 | 88888
如示例数据所示,dict值是有效的json字符串。我建议将它们转换为jsonb对象;然后,您可以使用
jsonb\u object\u keys()
提取键,对它们进行排序,并检索对应于最大键的值:

select z.ts, (t.dict::jsonb) ->> z.ts val
from t
cross join lateral (
    select ts
    from jsonb_object_keys(t.dict::jsonb) x(ts)
    order by ts desc
    limit 1
) z

with t as (select '{"2018-08-12T00:00:00": 88888.8, "2018-08-12T15:00:00": 88888.8, "2018-08-12T03:00:00": 88888.8, "2018-08-12T12:00:00": 88888.8, "2018-08-12T21:00:00": 88888.8, "2018-08-12T18:00:00": 88888.8, "2018-08-12T06:00:00": 88888.8, "2018-08-12T09:00:00": 88888.8}' dict)
select z.ts, (t.dict::jsonb) ->> z.ts val
from t
cross join lateral (
    select ts
    from jsonb_object_keys(t.dict::jsonb) x(ts)
    order by ts desc
    limit 1
) z
塔斯瓦尔 :------------------ | :------ 2018-08-12T21:00:00 | 88888
该列的数据类型是什么?@GMB包含dict字符串表示的列的类型是:varchar该列的数据类型是什么?@GMB包含dict字符串表示的列的类型是:varchar