[英]sql how to select count of multiple rows where row = value
hi im making a sql select for the count of multiple row values where Domain == 'whatever'.嗨,我正在制作一个 sql select 来计算多行值,其中 Domain == 'whatever'。
at the moment im making 5 seperate selects which take long to load and are inefficient, im asking if and how i can select the count of multiple rows in one query or atleast more efficiently than im doing now.目前我做了 5 个单独的选择,这些选择需要很长时间才能加载并且效率低下,我问我是否以及如何 select 在一个查询中计算多行的计数,或者至少比我现在做的更有效。
i am trying to select the count of rows: route, browser, device, location and referrer where the domain is equal to "whatever"我正在尝试 select 行数:路由、浏览器、设备、位置和引荐来源网址,其中域等于“任何”
i would like the output to contain all the listed rows above in a format like this: rowname: {count: 1}我希望 output 以如下格式包含上面列出的所有行:rowname: {count: 1}
here is the table:这是表格:
CREATE TABLE IF NOT EXISTS hits (
ID INTEGER PRIMARY KEY AUTO_INCREMENT,
Domain TEXT NOT NULL,
Route TEXT NOT NULL,
Timestamp INTEGER NOT NULL,
Browser TEXT,
Location TEXT,
Device TEXT,
Referrer TEXT
)
and these are the selects i have at the moment:这些是我目前的选择:
routes = db.run('SELECT Route, COUNT(Route) AS count FROM hits WHERE Domain = %s AND Timestamp BETWEEN %s AND %s GROUP BY Route', (domain, d1, d2))
browsers = db.run('SELECT Browser, COUNT(Browser) AS count FROM hits WHERE Domain = %s AND Timestamp BETWEEN %s AND %s GROUP BY Browser', (domain, d1, d2))
locations = db.run('SELECT Location, COUNT(Location) AS count FROM hits WHERE Domain = %s AND Timestamp BETWEEN %s AND %s GROUP BY Location', (domain, d1, d2))
screens = db.run('SELECT Device, COUNT(Device) AS count FROM hits WHERE Domain = %s AND Timestamp BETWEEN %s AND %s GROUP BY Device', (domain, d1, d2))
referrals = db.run('SELECT Referrer, COUNT(Referrer) AS count FROM hits WHERE Domain = %s AND Timestamp BETWEEN %s AND %s GROUP BY Referrer', (domain, d1, d2))```
The following works in SQL fiddle.以下作品在SQL fiddle。 The question is tagged with mysql and python, so please specify further what engine you are using if you need further assistance:
这个问题被标记为 mysql 和 python,所以如果您需要进一步的帮助,请进一步说明您使用的是什么引擎:
SELECT
COUNT(Route) AS route_count,
COUNT(Browser) AS browser_count,
COUNT(Location) AS location_count,
COUNT(Device) AS device_count,
COUNT(Referrer) AS referrer_count
FROM
hits
WHERE
Domain = 'whatever'
AND Timestamp BETWEEN '00:00' AND '12:00'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.