【问题标题】:How to find the key for the minimum value in jsonb column of postgres?如何在postgres的jsonb列中找到最小值的键?
【发布时间】:2016-12-19 09:45:36
【问题描述】:

我需要在一个jsonb对象中找到最小值的key,我已经找到了最小值,需要在同一个查询中找到相同的key。

我正在使用的查询

SELECT id,min((arr ->> 2)::numeric) AS custom_value 
FROM ( 
   SELECT id, jdoc
   FROM table, 
        jsonb_each(column1) d (key, jdoc)
   ) sub,
     jsonb_each(jdoc) doc (key, arr) 
group by 1 

【问题讨论】:

    标签: sql postgresql jsonb


    【解决方案1】:

    这样就可以了。
    left join ... on 1=1 用于保留空 json 的 ID

    select      t.id
               ,j.key
               ,j.value
    
    from                            mytable t
    
                left join lateral  (select      j.key,j.value 
                                    from        jsonb_each(column1) as j 
                                    order by    j.value 
                                    limit       1
                                    ) j
    
                on 1=1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-20
      • 2011-07-16
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-10
      相关资源
      最近更新 更多