mysql group by 组内排序不生效
表定义如下:
CREATE TABLE `test` (
`id` int NOT NULL AUTO_INCREMENT ,
`sale_date` date NOT NULL ,
`store_id` varchar(64) NOT NULL ,
`money` decimal(10,2) NOT NULL ,
`type` tinyint(4) NOT NULL ,
PRIMARY KEY (`id`)
);
插入数据:
2020-03-12 111 500 1
2020-03-12 111 800 2
2020-03-12 222 900 2
我现在想按照sale_date和store_id同时分组,完后组内是按type降序,在往上找了很多方案,都不生效,我的sql如下:
select * from(
select * from test order by type desc) r group by sale_date, store_id;
得到结果如下:
2020-03-12 111 500 1
2020-03-12 222 900 2
这是有问题的,其中第一条数据,我想要的money是800,因为type为2比1要大