不同值的频率 SQL
Frequency of distinct values SQL
有没有办法 (1) 将一列中的值(以秒为单位)转换为分钟,然后 (2) 获取不同值(分钟)的 frequency/occurrence?
我在第 1 步中使用了这个:
SELECT
ROUND(tripduration/60) AS duration
FROM
TABLE
这给了我以分钟为单位的持续时间,如下所示:
持续时间
11
16
11
...
11
但是对于第 2 步,我如何获得值出现频率的总和,以降序排列如下:
持续时间
频率
11
4000
13
3900
16
2500
...
...
14
230
可以使用聚合,如果我没理解错的话:
SELECT ROUND(tripduration/60) AS duration, COUNT(*) as frequency
FROM TABLE
GROUP BY ROUND(tripduration/60)
ORDER BY COUNT(*) DESC;
如果 tripduration 列出错,试试这个:
SELECT duration, frequency FROM (SELECT duration , COUNT(*) as frequency FROM (SELECT ROUND(tripduration/60,2) AS duration FROM TABLE) d GROUP BY duration) t ORDER BY frequency DESC;
有没有办法 (1) 将一列中的值(以秒为单位)转换为分钟,然后 (2) 获取不同值(分钟)的 frequency/occurrence?
我在第 1 步中使用了这个:
SELECT
ROUND(tripduration/60) AS duration
FROM
TABLE
这给了我以分钟为单位的持续时间,如下所示:
持续时间 |
---|
11 |
16 |
11 |
... |
11 |
但是对于第 2 步,我如何获得值出现频率的总和,以降序排列如下:
持续时间 | 频率 |
---|---|
11 | 4000 |
13 | 3900 |
16 | 2500 |
... | ... |
14 | 230 |
可以使用聚合,如果我没理解错的话:
SELECT ROUND(tripduration/60) AS duration, COUNT(*) as frequency
FROM TABLE
GROUP BY ROUND(tripduration/60)
ORDER BY COUNT(*) DESC;
如果 tripduration 列出错,试试这个:
SELECT duration, frequency FROM (SELECT duration , COUNT(*) as frequency FROM (SELECT ROUND(tripduration/60,2) AS duration FROM TABLE) d GROUP BY duration) t ORDER BY frequency DESC;