简体   繁体   English

sql 如何计算 select 行 = 值的多行数

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

相关问题 如何在 Pandas 中一起使用 groupby、select、count(*) 和 SQL 的命令 - How to use groupby, select, count(*) and where commands of SQL together in Pandas Python / Pandas:如何在一列中选择值等于另一列中另一行的行? - Python/Pandas: How do I select rows in one column where value iis equal to a different row in a different column? 在 Sql Alchemy 中,如何在 select 行中的任何列包含 substring? - In Sql Alchemy, how to select rows where any column contains a substring? Peewee:如何选择ID匹配列表的多行? - Peewee: how to select multiple rows where id matches the list? 如何选择行中至少一个元素中包含特定值的行? - How to select the rows that contain a specific value in at least one of the elements in a row? 如何打印 pandas 行中 select 特定值之后的所有行? - How print all rows after select specific value in row by pandas? Pandas 如何在 select 列中一行具有最大值? - Pandas how to select columns where a row has maximum value? Pandas value_count其中行名称 - Pandas value_count where row name 如何用 SELECT COUNT(*) 和 SQLAlchemy 计算行数? - How to count rows with SELECT COUNT(*) with SQLAlchemy? Pandas Groupby-如果多行超过另一行的值,则选择一列中值最高的行 - Pandas Groupby - select row with highest value in one column if multiple rows exceed value in another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM