- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我想使用 array_agg在子查询中,然后在我的主查询中通过它的数组索引使用聚合数据,但是,在尝试了许多不同的方法之后,我真的不知道应该怎么做;有人可以解释为什么在下面的示例中我得到了一系列 None 值而不是数组中的第一个类别吗?
我知道下面的简化示例可以在不对数组 [i] 执行 SELECT 的情况下完成,但它将解释问题的性质:
from sqlalchemy import Integer
from sqlalchemy.dialects.postgres import ARRAY
prods = (
session.query(
Product.id.label('id'),
func.array_agg(ProductCategory.id, type_=ARRAY(Integer)).label('cats'))
.outerjoin(
ProductCategory,
ProductCategory.product_id == Product.id)
.group_by(Product.id).subquery()
)
# Confirm that there's categories:
[x for x in session.query(prods) if len(x[1]) > 1][:10]
"""
Out[48]:
[(2428, [1633667, 1633665, 1633666]),
(2462, [1162046, 1162043, 2543783, 1162045]),
(2573, [1633697, 1633696]),
(2598, [2546824, 922288, 922289]),
(2645, [2544843, 338411]),
(2660, [1633713, 1633714, 1633712, 1633711]),
(2686, [2547480, 466995, 466996]),
(2748, [2546706, 2879]),
(2785, [467074, 467073, 2545804]),
(2806, [2545326, 686295, 686298, 686297])]
"""
# Ok now try to query to get the first category of each array:
[x for x in session.query(prods.c.cats[0].label('first_cat'))]
"""
(None),
(None),
(None),
(None),
(None),
(None),
(None),
(None),
(None),
(None),
(None),
"""
最佳答案
您可能做对了所有事情,但首先获取空数组。在您之前的查询中,您使用了 in-python 过滤 (len(x[1]) > 1
)。您可以在执行之前打印 Query
表达式以确保。
您可能应该为您的基本查询添加一个 having
子句:
from sqlalchemy import Integer
from sqlalchemy.dialects.postgresql import ARRAY
cats_agg = func.array_agg(ProductCategory.id, type_=ARRAY(Integer)).label('cats')
prods = (
session.query(
Product.id.label('id'),
cats_agg,
.outerjoin(
ProductCategory,
ProductCategory.product_id == Product.id)
.group_by(Product.id)
.having(func.array_length(cats_agg, 1) > 1)
.subquery()
)
那么你也不需要 in-python 过滤。
关于python - SQLAlchemy、PostgreSQL 和 array_agg : How to select items from array_agg?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23261944/
t1 id|entity_type 9|3 9|4 9|5 2|3 2|5 t2 id|entity_type 1|3 1|4 1|5 SELECT t1.id, array_agg(t1.
我有一个 SQL 表 message(application, type, action, date, ...) 我想获取一个类型的所有操作和一个应用程序的所有类型如果可能,单个查询。 到目前为止,我
我有以下查询: SELECT DISTINCT ON (ps.p) m.groundtruth, ps.p, ARRAY_AGG(m.anchor_id), ARRAY_AGG(m.id) FRO
我想使用 array_agg在子查询中,然后在我的主查询中通过它的数组索引使用聚合数据,但是,在尝试了许多不同的方法之后,我真的不知道应该怎么做;有人可以解释为什么在下面的示例中我得到了一系列 Non
我正在尝试连接一个表,该表可能具有多个给定 ID 的条目,并将与该 ID 对应的行聚合到一个数组中。这在 SQL 查询中看起来如下: SELECT * from data LEFT JOIN (sel
我有一张表格,其中列出了潜在客户、他们的事件和他们的销售代表。每个客户最多可以有 1 名销售代表。我建立了一个汇总表,在其中汇总客户事件,并按销售代表对其进行分组,并按客户创建日期进行筛选。这不是一个
我在带有 Java-8 的 Ubuntu 10.6-1.pgdg18.04+1 上使用 PostgreSQL 10.6。我有两个表: 表格测试 Column | Type
是否有任何替代 PostgreSQL array_agg() 函数的方法,以便它不会以以下格式返回值:'{x,y,z,}' . 我可以让它只返回:'x,y,z' 吗? 最佳答案 在 PostgreSQ
我目前有一个这样的示例标记表: id | tag | user_id | video_id 1 | cake | 1 | 1 2 | cookie |
作为测试,我创建了这个模式: CREATE TABLE simple_table (client_id int4, order_id int4); INSERT INTO simple_table (
我正在尝试创建一个返回字符串数组的函数,我可以在没有函数的情况下执行此操作并返回 record[] 类型,当我尝试返回该类型时导致它表示不支持的功能。 CREATE OR REPLACE FUNCTI
我有以下 Postgres 查询: SELECT array_agg("Esns".id ) FROM public."Esns", public."PurchaseOrderItems"
我正在尝试让 array_agg 与 Postgresql 中的数组类型一起工作,但我无法弄清楚这是否可行,如果可行,该怎么做。我查询的相关部分如下所示: array_agg(ARRAY[e.aler
表“动物”: animal_name animal_type Tom Cat Jerry Mouse Kermit Frog 查询: SELECT array_t
我正在尝试创建一个具有不同结构的数组作为列的值,类似这样 select array_agg(distinct struct(field_a, field_b)) as c FROM tables ..
我在 postgres 中有一对多的关系(事件有很多 EventUser),并且想扫描并存储到结构 Event . // EventUser struct type EventUser struct
我想为我的表中的每个用户选择不同的列值(在 Google BigQuery 中)。我想过使用 ARRAY_AGG()喜欢: SELECT user_id, ARRAY_AGG(DISTINCT fie
如何计算由 ARRAY_AGG() 在 PostgresQL 中创建的数组对象中不同元素的数量?这是一个用于讨论目的的玩具示例: SELECT ARRAY_AGG (first_name || ' '
考虑这个例子: SELECT comment_date , array_agg(user_id) users , array_agg(comment) comme
我有一个表 calls 和 calls_statistics。 calls 有一个主键calls_id,它是calls_statistics 中的一个外键。 调用当前包含 16k 个条目。 当我运行时
我是一名优秀的程序员,十分优秀!