[英]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.