如何按天排序,然后按总和排序

qxgroojn  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(367)

我的表格的每一行都是一个应用程序(google,facebook),上面有日期和字节。
我所做的是按应用程序和日期对请求进行分组:

SELECT app, sum(bytes) as total, date
FROM logs
WHERE `date` BETWEEN '2018-11-23' AND '2018-11-25'
GROUP BY app, date

结果如下:

app      | total  | date
facebook | 2736   | 2018-11-23
google   | 4968   | 2018-11-23
twitter  | 606520 | 2018-11-23
facebook | 2736   | 2018-11-24
youtube  | 153077 | 2018-11-24
twitter  | 84561  | 2018-11-24

但我需要的是在desc中排序total。所以现在我尝试每天按total排序,得到以下结果:

app      | total  | date
twitter  | 606520 | 2018-11-23 <---
google   | 4968   | 2018-11-23 <---
facebook | 2736   | 2018-11-23
youtube  | 153077 | 2018-11-24 <---
twitter  | 84561  | 2018-11-24 <---
facebook | 2736   | 2018-11-24

你知道如何使用mysql吗?谢谢!

djp7away

djp7away1#

你的样本数据告诉我:

SELECT app, sum(bytes) as total, date
FROM logs
WHERE `date` BETWEEN '2018-11-23' AND '2018-11-25'
GROUP BY app, date
ORDER BY date, total DESC;
kzipqqlq

kzipqqlq2#

你可以在下面使用 order by total desc and date asc ```
SELECT app, sum(bytes) as total, date
FROM logs
WHERE date BETWEEN '2018-11-23' AND '2018-11-25'
GROUP BY app, date
order by date,total desc

相关问题