GVKun编程网logo

Postgres中的sql – GROUP BY – JSON数据类型不相等?

9

在本文中,我们将详细介绍Postgres中的sql–GROUPBY–JSON数据类型不相等?的各个方面,同时,我们也将为您带来关于JDBC,Elasticsearch和PostgresqlJson数据

在本文中,我们将详细介绍Postgres中的sql – GROUP BY – JSON数据类型不相等?的各个方面,同时,我们也将为您带来关于JDBC,Elasticsearch和Postgresql Json数据类型、jsonb内部字段上的Postgres GROUP BY、Postgres JSON数据类型Rails查询、PostgreSQL 9.4 RC1 发布,JSONB 数据类型的有用知识。

本文目录一览:

Postgres中的sql – GROUP BY – JSON数据类型不相等?

Postgres中的sql – GROUP BY – JSON数据类型不相等?

我在匹配表中有以下数据:
5;{"Id":1,"Teams":[{"Name":"TeamA","Players":[{"Name":"AAA"},{"Name":"BBB"}]},{"Name":"TeamB","Players":[{"Name":"CCC"},{"Name":"DDD"}]}],"TeamRank":[1,2]}
6;{"Id":2,2]}

我想选择每个最后一个不同的团队在他们的名字.即我想要一个返回的查询:

6;{"Name":"TeamA",{"Name":"BBB"}
6;{"Name":"TeamB",{"Name":"DDD"}

所以每个团队从上次那个团队出现在表中.
我一直在使用以下(从here):

WITH t AS (SELECT id,json_array_elements(match->'Teams') AS team FROM matches)
SELECT MAX(id) AS max_id,team FROM t GROUP BY team->'Name';

但这返回:

06003

我明白Postgres doesn’t have equality for JSON.我只需要平等的球队名称(一个字符串),那个球队的球员不需要比较.

任何人都可以提出替代方法来做到这一点吗?
以供参考:

SELECT id,json_array_elements(match->'Teams') AS team FROM matches

收益:

5;"{"Name":"TeamA",{"Name":"BBB"}]}"
5;"{"Name":"TeamB",{"Name":"DDD"}]}"
6;"{"Name":"TeamA",{"Name":"BBB"}]}"
6;"{"Name":"TeamB",{"Name":"DDD"}]}"

编辑:我转换为文本,this question后,我使用disTINCT ON而不是GROUP BY.这是我的完整查询:

WITH t AS (SELECT id,json_array_elements(match->'Teams') AS team
           FROM matches ORDER BY id DESC)
SELECT disTINCT ON (team->>'Name') id,team FROM t;

返回上面我想要的有没有人有更好的解决方案?

解决方法

更加快速,更优雅的LAteraL加入:
SELECT disTINCT ON (t.team->>'Name') t.team
FROM   matches m,json_array_elements(m.match->'Teams') t(team);
ORDER  BY t.team->>'Name',m.id DESC;  -- to get the "last"

如果你只想要不同的团队,ORDER BY可以去.有关:

> Query for element of array in JSON column
> Query for array elements inside JSON type

JSON和平等

在Postgres中没有json数据类型的等号运算符,但jsonb(Postgres 9.4)有一个:

> How to query a json column for empty objects?

JDBC,Elasticsearch和Postgresql Json数据类型

JDBC,Elasticsearch和Postgresql Json数据类型

  • PostgreSQL:9.3.2
    • Elasticsearch:0.90
    • jprante / elasticsearch-river-jdbc:2.2.2
    • PostgreSQL JDBC:9.3-1100 JDBC 41

我正在尝试使用elasticsearch river将一个postgresql Json数据类型列放入elasticsearch中。

这里是创造的河

curl -XPUT 'localhost:9200/_river/business_river/_meta' -d '{
          "type" : "jdbc","jdbc" : {
              "driver" : "org.postgresql.Driver","url" : "jdbc:postgresql://localhost:5432/business","user" : "postgres","password" : "","sql" : "select id,active,companies->'sic'->>'national_number' AS sic,companies->'names'->>'name' AS name,companies->'address'->>'country' AS country from businesses.business","index" : "business","type" : "jdbc"
     }
}'

河边抱怨说“名称”列不存在!该查询在Postgresql中工作,这使我认为JDBC或river不支持Json数据类型。

这是完整的消息

[2014-01-13 07:47:27,919][INFO ][org.xbib.elasticsearch.river.jdbc.JDBCRiver] [Brigade] [jdbc][business_river] starting JDBC river: URL [jdbc:postgresql://localhost:5432/business],driver [org.postgresql.Driver],strategy [oneshot],index [jdbc]/[jdbc]
[2014-01-13 07:47:33,281][ERROR][org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverFlow] ERROR: column "names" does not exist
  Position: 31
org.postgresql.util.PSQLException: ERROR: column "names" does not exist
  Position: 31
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2161)
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1890)
        at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)                                                                                         
        at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:560)                                                                                 
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:403)                                                                        
        at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:283)                                                                            
        at org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverSource.executeQuery(SimpleRiverSource.java:417)                                                         
        at org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverSource.fetch(SimpleRiverSource.java:241)                                                                
        at org.xbib.elasticsearch.river.jdbc.strategy.simple.SimpleRiverFlow.move(SimpleRiverFlow.java:184)                                                                     
        at org.xbib.elasticsearch.river.jdbc.strategy.oneshot.OneShotRiverFlow.run(OneShotRiverFlow.java:38)                                                                    
        at java.lang.Thread.run(Thread.java:744)

jsonb内部字段上的Postgres GROUP BY

jsonb内部字段上的Postgres GROUP BY

我正在使用Postgresql 9.4,并具有一个表test,带有id::intcontent::jsonb,如下所示:

 id |     content
----+-----------------
  1 | {"a": {"b": 1}}
  2 | {"a": {"b": 1}}
  3 | {"a": {"b": 2}}
  4 | {"a": {"c": 1}}

如何GROUP BYcontent列的内部字段上将每个组作为数组返回?具体来说,我正在寻找的结果是:

             content
---------------------------------
[{"a": {"b": 1}},{"a": {"b": 1}}]
[{"a": {"b": 2}}]
(2 rows)

试:

SELECT json_agg(content) as content FROM test GROUP BY content ->> '{a,b}';

产量:

                               content
----------------------------------------------------------------------
[{"a": {"b": 1}},{"a": {"b": 1}},{"a": {"b": 2}},{"a": {"c": 1}}]
(1 row)

Postgres JSON数据类型Rails查询

Postgres JSON数据类型Rails查询

我正在使用Postgres的json数据类型,但想对嵌套在json中的数据进行查询/排序。

我想订购或查询json数据类型上的.where。例如,我要查询关注者计数> 500的用户,或者要按关注者或关注计数进行订购。

谢谢!

例:

model Userdata: {     "photos"=>[       {"type"=>"facebook", "type_id"=>"facebook", "type_name"=>"Facebook", "url"=>"facebook.com"}      ],      "social_profiles"=>[         {"type"=>"vimeo", "type_id"=>"vimeo", "type_name"=>"Vimeo", "url"=>"http://vimeo.com/", "username"=>"v", "id"=>"1"},         {"bio"=>"I am not a person, but a series of plants", "followers"=>1500, "following"=>240, "type"=>"twitter", "type_id"=>"twitter", "type_name"=>"Twitter", "url"=>"http://www.twitter.com/", "username"=>"123", "id"=>"123"}     ]}

答案1

小编典典

对于任何偶然发现的人。我想出了一个使用ActiveRecord和Postgres的JSON数据类型的查询列表。随时对其进行编辑以使其更加清晰。

以下使用的JSON运算符的文档:https : //www.postgresql.org/docs/current/functions-
json.html。

# Sort based on the Hstore data:Post.order("data->''hello'' DESC")=> #<ActiveRecord::Relation [    #<Post id: 4, data: {"hi"=>"23", "hello"=>"22"}>,     #<Post id: 3, data: {"hi"=>"13", "hello"=>"21"}>,     #<Post id: 2, data: {"hi"=>"3", "hello"=>"2"}>,     #<Post id: 1, data: {"hi"=>"2", "hello"=>"1"}>]># Where inside a JSON object:Record.where("data ->> ''likelihood'' = ''0.89''")# Example json object:r.column_data=> {"data1"=>[1, 2, 3],     "data2"=>"data2-3",     "array"=>[{"hello"=>1}, {"hi"=>2}],     "nest"=>{"nest1"=>"yes"}}# Nested search:Record.where("column_data -> ''nest'' ->> ''nest1'' = ''yes'' ")# Search within array:Record.where("column_data #>> ''{data1,1}'' = ''2'' ")# Search within a value that''s an array:Record.where("column_data #> ''{array,0}'' ->> ''hello'' = ''1'' ")# this only find for one element of the array.# All elements:Record.where("column_data ->> ''array'' LIKE ''%hello%'' ") # badRecord.where("column_data ->> ''array'' LIKE ?", "%hello%") # good

PostgreSQL 9.4 RC1 发布,JSONB 数据类型

PostgreSQL 9.4 RC1 发布,JSONB 数据类型

PostgreSQL 9.4 RC1 发布,此版本现已提供下载,文档请前往这里查看。

主要更新内容请看:

  • Allow materialized views to be refreshed without blocking reads

  • Add support for logical decoding of WAL data, to allow database changes to be streamed out in a customizable format

  • Allow background worker processes to be dynamically registered, started and terminated

  • Add jsonb, a more capable and efficient data type for storingJSONdata

  • Add newSQLcommand ALTER SYSTEM for updating postgresql.conf configuration file entries

  • Reduce lock strength for some ALTER TABLE commands

更多内容请看:Postgres 9.4 重要特性:jsonb 数据类型

完整改进内容请看发行说明,其他描述或者最新特性请看 9.4 Features Wiki Page。

PostgreSQL 依赖于社区帮忙测试下一个版本,保证其高性能的特性。更多测试相关信息请看这里。

关于Postgres中的sql – GROUP BY – JSON数据类型不相等?的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于JDBC,Elasticsearch和Postgresql Json数据类型、jsonb内部字段上的Postgres GROUP BY、Postgres JSON数据类型Rails查询、PostgreSQL 9.4 RC1 发布,JSONB 数据类型的相关知识,请在本站寻找。

本文标签: