python - 联合子查询的 SqlAlchemy groupby
问题描述
我需要在同一张表上做 3 个查询,将它们合并,然后用 sum 做一个 groupby。
这是我的解决方案。
q1 = Contact.query.with_entities(Contact.name, Contact.surname, Contact.company, literal(1).label('weight')).filter(Contact.name.ilike(expr))
q2 = Contact.query.with_entities(Contact.name, Contact.surname, Contact.company, literal(2).label('weight')).filter(Contact.surname.ilike(expr))
q3 = Contact.query.with_entities(Contact.name, Contact.surname, Contact.company, literal(3).label('weight')).filter(Contact.company.ilike(expr))
qq = q1.union_all(q2,q3).subquery()
w = db.session.query(qq.c.name, qq.c.surname, qq.c.company, func.sum(qq.c.weight)).group_by(qq.c.name).all()
它引发以下错误
Traceback (most recent call last)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 210, in __getattr__
return self._data[key]
During handling of the above exception, another exception occurred:
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask/app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/flask_login/utils.py", line 272, in decorated_view
return func(*args, **kwargs)
File "/home/user1/pyweb/profc/app/routes_contact.py", line 42, in contact
w = db.session.query(qq.c.name, qq.c.surname, qq.c.company, func.sum(qq.c.weight)).group_by(qq.c.name).all()
File "/home/user1/pyweb/profc/env/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 212, in __getattr__
[Open an interactive python shell in this frame] raise AttributeError(key)
AttributeError: name
我哪里错了?
*编辑:已解决*
问题是联合后的名称,我解决了标记每一列
q1 = Contact.query.with_entities(Contact.name.label('uname'), Contact.surname.label('usurname'), Contact.company.label('ucompany'), literal(1).label('weight')).filter(Contact.name.ilike(expr))
q2 = Contact.query.with_entities(Contact.name.label('uname'), Contact.surname.label('usurname'), Contact.company.label('ucompany'), literal(2).label('weight')).filter(Contact.surname.ilike(expr))
q3 = Contact.query.with_entities(Contact.name.label('uname'), Contact.surname.label('usurname'), Contact.company.label('ucompany'), literal(3).label('weight')).filter(Contact.company.ilike(expr))
qq = q1.union_all(q2,q3).subquery()
w = db.session.query(qq.c.uname, qq.c.usurname, qq.c.ucompany, func.sum(qq.c.weight)).group_by(qq.c.uname).all()
解决方案
推荐阅读
- java - Spring aop:找不到引用的切入点注释
- c# - Grpc - 从一个客户端向连接到同一服务器的另一个客户端发送消息
- loops - 在 Pyspark 中循环导致 sparkException
- java - 如何使用 java 将数据从 blob 数据类型从 oracle 迁移到 SQL 在文件流中我只动态发送表
- angular - 在 Angular CLI 中使用 ng new 命令出错
- reactjs - React-picky:元素类型无效:期望字符串(对于内置组件)或类/函数(对于复合组件)但得到:未定义
- java - 为什么 Jackson TypeReference 是抽象的?
- r - 将反应值从服务器传递到闪亮的ui中的两个条件面板(未嵌套)?
- android - 领域 - 在查询构建时访问字段名称而无需硬编码
- git - git push 没有任何问题