【发布时间】:2021-06-10 05:30:39
【问题描述】:
我有这样的数据表
| price | date | type |
|---|---|---|
| 1000 | 2021-03-13 | A |
| 1000 | 2021-03-14 | A |
| 1000 | 2021-03-15 | A |
| 1300 | 2021-03-16 | A |
| 1000 | 2021-03-17 | A |
| 1300 | 2021-03-18 | A |
| 1000 | 2021-03-19 | A |
| 1500 | 2021-03-20 | A |
| 1500 | 2021-03-21 | A |
| 1500 | 2021-03-22 | A |
| 2000 | 2021-03-13 | B |
| 2000 | 2021-03-14 | B |
| 2000 | 2021-03-15 | B |
| 2000 | 2021-03-16 | B |
| 2000 | 2021-03-17 | B |
我想查询这样的数据
| price | startDate | endDate | type |
|---|---|---|---|
| 1000 | 2021-03-13 | 2021-03-15 | A |
| 1300 | 2021-03-16 | 2021-03-16 | A |
| 1000 | 2021-03-17 | 2021-03-17 | A |
| 1300 | 2021-03-18 | 2021-03-18 | A |
| 1000 | 2021-03-19 | 2021-03-19 | A |
| 1500 | 2021-03-20 | 2021-03-22 | A |
| 2000 | 2021-03-13 | 2021-03-17 | B |
下面是我当前的sql,但它根本不正确
SELECT MIN(date) as startDate, MAX(date) as endDate, price, type
FROM prices
GROUP BY type, price
ORDER BY type, MIN(date)
【问题讨论】:
-
你的mysql版本是多少?
-
您如何确保数据按此顺序显示?
-
什么是空白没有被另一条记录填补,即当记录 (1300,2021-03-16,A) 不存在时?