仅获取每个代码的最近日期的记录
Get only the records with most recent date to every code
我有一个table这样的
+------+-------+-----------+-------------+
| code | price | perct_off | date_review |
+------+-------+-----------+-------------+
| 0001 | 1500 | 40 | 2017-12-30 |
| 0001 | 1500 | 40 | 2018-02-15 |
| 0001 | 2000 | 25 | 2018-07-31 |
| 0002 | 3000 | 45 | 2018-03-20 |
| 0002 | 5000 | 20 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+------+-------+-----------+-------------+
而且我只想记录每个代码的最大日期。输入必须是:
+------+-------+-----------+-------------+
| code | price | perct_off | date_review |
+------+-------+-----------+-------------+
| 0001 | 2000 | 25 | 2018-07-31 |
| 0002 | 5000 | 20 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+------+-------+-----------+-------------+
当我尝试时:
SELECT DISTINCT
(code), price, perct_off, MAX(date_review)
FROM `table01`
GROUP BY code
我得到了这个输出
+-------+--------+------------+-------------------+
| code | price | perct_off | max(date_review) |
+-------+--------+------------+-------------------+
| 0001 | 1500 | 40 | 2018-07-31 |
| 0002 | 3000 | 45 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+-------+--------+------------+-------------------+
如何获得正确的输出?
提前致谢。
执行此操作的一种规范方法是加入子查询,该子查询为每个 code
:
找到最近的日期
SELECT t1.*
FROM table01 t1
INNER JOIN
(
SELECT code, MAX(date_review) AS max_date_review
FROM table01
GROUP BY code
) t2
ON t1.code = t2.code AND t1.date_review = t2.max_date_review;
优化方案:
SELECT
t1.code,
t1.price,
t1.perct_off,
t1.date_review
FROM t t1
LEFT JOIN t t2 ON
t1.code = t2.code
AND t1.date_review < t2.date_review
WHERE t2.date_review IS NULL;
我有一个table这样的
+------+-------+-----------+-------------+
| code | price | perct_off | date_review |
+------+-------+-----------+-------------+
| 0001 | 1500 | 40 | 2017-12-30 |
| 0001 | 1500 | 40 | 2018-02-15 |
| 0001 | 2000 | 25 | 2018-07-31 |
| 0002 | 3000 | 45 | 2018-03-20 |
| 0002 | 5000 | 20 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+------+-------+-----------+-------------+
而且我只想记录每个代码的最大日期。输入必须是:
+------+-------+-----------+-------------+
| code | price | perct_off | date_review |
+------+-------+-----------+-------------+
| 0001 | 2000 | 25 | 2018-07-31 |
| 0002 | 5000 | 20 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+------+-------+-----------+-------------+
当我尝试时:
SELECT DISTINCT
(code), price, perct_off, MAX(date_review)
FROM `table01`
GROUP BY code
我得到了这个输出
+-------+--------+------------+-------------------+
| code | price | perct_off | max(date_review) |
+-------+--------+------------+-------------------+
| 0001 | 1500 | 40 | 2018-07-31 |
| 0002 | 3000 | 45 | 2018-08-01 |
| 0003 | 3000 | 40 | 2018-01-16 |
+-------+--------+------------+-------------------+
如何获得正确的输出?
提前致谢。
执行此操作的一种规范方法是加入子查询,该子查询为每个 code
:
SELECT t1.*
FROM table01 t1
INNER JOIN
(
SELECT code, MAX(date_review) AS max_date_review
FROM table01
GROUP BY code
) t2
ON t1.code = t2.code AND t1.date_review = t2.max_date_review;
优化方案:
SELECT
t1.code,
t1.price,
t1.perct_off,
t1.date_review
FROM t t1
LEFT JOIN t t2 ON
t1.code = t2.code
AND t1.date_review < t2.date_review
WHERE t2.date_review IS NULL;