我有一个表格“ orders”,如下所示:
+---------------+--------------+------------+
| customer_name | order_number | date |
+---------------+--------------+------------+
| jack | 1 | 2018-01-01 |
| jack | 2 | 2018-01-06 |
| jack | 3 | 2018-01-19 |
| jack | 4 | 2018-01-06 |
| jack | 5 | 2018-02-27 |
| jack | 6 | 2018-02-02 |
+---------------+--------------+------------+
现在,我想要一个表,该表可以显示连续日期(以天为单位)之间的差异。像这样:
+------------+------------+------+
| date | next_date | diff |
+------------+------------+------+
| 2018-01-01 | 2018-01-06 | 5 |
| 2018-01-06 | 2018-01-06 | 0 |
| 2018-01-06 | 2018-01-19 | 13 |
| 2018-01-19 | 2018-02-02 | 14 |
| 2018-02-02 | 2018-02-27 | 25 |
+------------+------------+------+
我使用的查询是这样:
SELECT orders.date, MIN(table1.date) FROM orders
LEFT JOIN orders table1
on orders.customer_name = table1.customer_name
AND table1.date >= orders.date
AND table1.order_number != orders.order_number
WHERE orders.customer_name = 'jack'
GROUP BY orders.order_number, orders.date
ORDER BY orders.date;
这是输出:
+------------+------------+
| date | next_date |
+------------+------------+
| 2018-01-01 | 2018-01-06 |
| 2018-01-06 | 2018-01-06 |
| 2018-01-06 | 2018-01-06 |
| 2018-01-19 | 2018-02-02 |
| 2018-02-02 | 2018-02-27 |
| 2018-02-27 | NULL |
+------------+------------+
如您所见,这里有一些问题。
date
和next_date
均为2018-01-06
。next_date
所在的行没有2018年1月19日的行next_date
值为NULL 我知道这是因为我已按order_number和>=
分组
但我不知道该如何处理。我觉得有一个显而易见的简单解决方案正在逃避我。有帮助吗?
以防SQL Fiddle无法正常工作:
CREATE TABLE orders
(`customer_name` varchar(4), `order_number` int, `date` varchar(10))
;
INSERT INTO orders
(`customer_name`, `order_number`, `date`)
VALUES
('jack', 1, '2018-01-01'),
('jack', 2, '2018-01-06'),
('jack', 3, '2018-01-19'),
('jack', 4, '2018-01-06'),
('jack', 5, '2018-02-27'),
('jack', 6, '2018-02-02')
;
答案 0 :(得分:1)
Row_Number()
功能。orders
表两次用于两个不同的Derived Tables中,并根据date
的升序分配行号。两者之间的区别在于,该表之一将具有已修改的行号(增加1)。DateDiff()
函数来确定日期之间的差异。请尝试以下操作:
SELECT t.`date`,
next_t.`date` AS next_date,
DATEDIFF(next_t.`date`, t.`date`) AS diff
FROM
(
SELECT 1 + (ROW_NUMBER() OVER (ORDER BY `date` ASC)) AS rn,
`date`
FROM orders
WHERE customer_name = 'jack'
) AS t
JOIN
(
SELECT (ROW_NUMBER() OVER (ORDER BY `date` ASC)) AS rn,
`date`
FROM orders
WHERE customer_name = 'jack'
) AS next_t ON next_t.rn = t.rn
答案 1 :(得分:1)
您可以这样做。但是您的order 4
比previous date
有order 3
。因此它将产生负数。
SELECT customer_name,order_number,date,
LEAD(date) OVER (ORDER BY customer_name,order_number) next_date,
ISNULL(DATEDIFF(DAY,date,LEAD(date) OVER (ORDER BY customer_name,order_number)),0) AS diff
FROM #orders
答案 2 :(得分:1)
如果您对基于 MySQL 8.x 或更高版本的非 ROW_NUMBER()解决方案感兴趣,请查看以下说明。
EXPLANATION:
1)首先,我们从 orders 表中选择所有日期,通过升序排列并为每个日期分配一个虚拟的自动增量ID来对它们进行排序。我们将得到这样的东西:
_flutterWebviewPlugin.getCookies().then((Map<String, String> cookies) {
Map<String, String> trimCookies = {};
for (String key in cookies.keys) {
trimCookies[key.replaceAll("\"", "").trim()] = cookies[key].replaceAll("\"", "");
}
}
2)我们创建与上一个查询类似的查询,但是这次我们丢弃第一行,如下所示:
SELECT (@row_number := @row_number + 1) AS orderNum, date
FROM ORDERS, (SELECT @row_number:=0) AS t
ORDER BY date;
Output:
1 2018-01-01
2 2018-01-06
3 2018-01-06
4 2018-01-19
5 2018-02-02
6 2018-02-27
这里唯一的问题是,我们必须将LIMIT号硬编码为足够高的数字,因此我们可以确保选择除第一行之外的所有行。
3)至此,您应该考虑通过虚拟生成的ID将之前的两个结果结合在一起。因此,让我们看一下最终查询:
SELECT (@row_number2 := @row_number2 + 1) AS orderNum, date
FROM ORDERS, (SELECT @row_number2 := 0) AS t
ORDER BY date
LIMIT 999999999999
OFFSET 1;
Output:
1 2018-01-06
2 2018-01-06
3 2018-01-19
4 2018-02-02
5 2018-02-27
您可以在此处查看工作示例:http://sqlfiddle.com/#!9/1572ea/27