我有一张这样的桌子;
+----+---------+-------------+
| id | user_id | screenWidth |
+----+---------+-------------+
| 1 | 1 | 1366 |
| 2 | 1 | 1366 |
| 3 | 1 | 1366 |
| 4 | 1 | 1366 |
| 5 | 2 | 1920 |
| 6 | 2 | 1920 |
| 7 | 3 | 1920 |
| 8 | 4 | 1280 |
| 9 | 5 | 1280 |
| 10 | 6 | 1280 |
+----+---------+-------------+
随着其他数据的加载。如果需要,这可以正常化,最初我认为我不需要,但也许我应该。无论如何,
我想要一个只为每个用户计算一次screenWidth值的查询,所以输出看起来像:
+-------------+-------+
| screenWidth | count |
+-------------+-------+
| 1366 | 1 |
| 1920 | 2 |
| 1280 | 3 |
+-------------+-------+
而不是将1366计为4 - 这样可以避免重度用户倾斜数据。
有没有办法编写查询来执行此操作?
答案 0 :(得分:6)
简短而简单:使用COUNT DISTINCT
:
SELECT
screenWidth,
COUNT(DISTINCT user_id)
FROM
mytable
GROUP BY
screenWidth;
答案 1 :(得分:5)
您必须获得每个屏幕宽度的DISTINCT用户数,这是获取结果的示例查询。
Click here to view the demo in SQL Fiddle
脚本:
CREATE TABLE screenwidth
(
id INT NOT NULL
, user_id INT NOT NULL
, screenwidth INT NOT NULL
);
INSERT INTO screenwidth (id, user_id, screenwidth) VALUES
(1, 1, 1366),
(2, 1, 1366),
(3, 1, 1366),
(4, 1, 1366),
(5, 2, 1920),
(6, 2, 1920),
(7, 3, 1920),
(8, 4, 1280),
(9, 5, 1280),
(10, 6, 1280);
SELECT screenwidth
, COUNT(DISTINCT user_id) AS screenwidthcount
FROM screenwidth
GROUP BY screenwidth
ORDER BY screenwidthcount;
输出:
SCREENWIDTH SCREENWIDTHCOUNT
----------- ----------------
1366 1
1920 2
1280 3