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

时间:2018-12-18 11:06:08

标签: mysql sql

表的每一行都是一个带有日期和字节的应用程序(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中订购总计。所以现在我正在尝试每天总计订购,并得到以下结果:

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的任何想法?谢谢!

2 个答案:

答案 0 :(得分:1)

您可以使用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

答案 1 :(得分:1)

您的样本数据提示我:

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;