简体   繁体   English

Sqlalchemy groupby 我怎么知道哪个值属于哪个组?

[英]Sqlalchemy groupby how do I know which value belongs to which group?

Say I have the following data:假设我有以下数据:

Clicks           Device
-------------------------
  1              Mobile
  2              Desktop
  1              Tablet
  1              Mobile       
 ..                ..

I am querying the table as follows:我正在查询该表,如下所示:

q = db_session.query(func.count(Website.Clicks)).group_by(Website.Device)


for a in q:
print(a)

The result is:结果是:

2
2
1

My question is how do I know which value belongs to which device?我的问题是我怎么知道哪个值属于哪个设备? So for example how can I query a or output the following:因此,例如,我如何查询 a 或 output 以下内容:

Mobile 2
Desktop 2
Tablet 1

Your query only projects the count of Website Clicks, so that's all you're getting.您的查询仅预测网站点击次数,这就是您所得到的。 Add the Devices to the projection:将设备添加到投影:

q = db_session.query(Website.Device, func.count(Website.Clicks)).group_by(Website.Device)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让用户属于python中的哪个组 - How to get the user belongs to which group in python 我怎么知道这个词属于哪个主题? - How do I know which topic this word comes in? 给定一个值,我如何知道它出现在哪些列中? - Given a value how can I know in which columns it is present? 如何使用Python查找我的数据属于哪个群集? - How do I find which cluster my data belongs to using Python? 我想在第一次出现时用一个字符分割一个字符串,该字符属于一个字符列表。 如何在python中做到这一点? - I want to split a string by a character on its first occurence, which belongs to a list of characters. How to do this in python? 你如何找出一个值属于哪个对象? - How can you find out which object a value belongs to? 查找值属于多个集合中的哪个 - Find in which of multiple sets a value belongs to 如何检查 SQLAlchemy 中的 Session 当前跟踪哪些对象? - How can I check which objects are currently tracked by the Session in SQLAlchemy? 如何对作为字典值的列表进行排序? - How do I sort a list which is a value of dictionaries? 在解决异常时,我如何知道哪个参数有错误消息? - How do i know which argument has error message when resolving exceptions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM