MySQL: por fin en el grupo de toma ningún efecto

gesanri:

Tengo una tabla como la siguiente:

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`)
);

y insertar los datos como a continuación:

2020-03-12 111 500 1
2020-03-12 111 800 2
2020-03-12 222 900 2

Quiero grupo por sale_date y store_id, y el orden grupo interno por tipo desc, My SQL es la siguiente:

select * from(
select * from test order by type desc) r group by sale_date, store_id;

pero el resultado es:

2020-03-12 111 500 1
2020-03-12 222 900 2

y el resultado se supone que es la siguiente:

2020-03-12 111 800 2
2020-03-12 222 900 2
pasarán;

Utilice una consulta correlacionada:

select t.* 
from test t
where t.type = (select max(type) from test where sale_date = t.sale_date and store_id = t.store_id)

Vea la demostración .
resultados:

| id  | sale_date  | store_id | money | type |
| --- | ---------- | -------- | ----- | ---- |
| 2   | 2020-03-12 | 111      | 800   | 2    |
| 3   | 2020-03-12 | 222      | 900   | 2    |

Supongo que te gusta

Origin http://43.154.161.224:23101/article/api/json?id=277738&siteId=1
Recomendado
Clasificación