0

我有一张桌子,上面有这样的订单:

+----------+-------------+------------+
| order_id | customer_id | order_date |
+----------+-------------+------------+
|    404   |     16      | 2006-11-04 |
|    726   |     16      | 2007-11-01 |
|    440   |     16      | 2010-03-15 |
|    515   |     16      | 2011-04-29 |
|    539   |     16      | 2011-11-01 |
|    293   |     89      | 2001-08-28 |
|    156   |     89      | 2002-05-03 |
|    238   |     89      | 2003-09-15 |
|    299   |     28      | 2001-10-26 |
|    314   |     28      | 2004-03-18 |
|    142   |     79      | 2002-01-21 |
|    313   |     12      | 2004-03-11 |
|    388   |     12      | 2005-11-22 |
|    507   |     12      | 2007-03-27 |
|    286   |     14      | 2006-04-17 |
|    200   |     14      | 2008-05-25 |
|    130   |     14      | 2009-06-24 |
|    527   |     19      | 2011-05-14 |
|    298   |     76      | 2001-10-20 |
|    165   |     76      | 2002-07-27 |
|    212   |     11      | 2003-02-17 |
|    412   |     11      | 2006-08-02 |
|    423   |     11      | 2007-03-28 |
|    388   |     10      | 2005-01-07 |
|    390   |     65      | 2006-06-07 |
|    44    |     65      | 2008-01-07 |
|    269   |     16      | 2008-02-25 |
|    413   |     14      | 2007-12-01 |
|    675   |     14      | 2007-09-11 |
|    260   |     14      | 2009-10-20 |
+----------+-------------+------------+

现在我需要一个查询,它给我一个这样的列表:

+-------------+------------+------------+------+
| customer_id |    date1   |    date2   | days |
+-------------+------------+------------+------+
|     16      | 2006-11-04 | 2007-11-01 | 362  |
|     16      | 2007-11-01 | 2010-03-15 | 865  |
|     16      | 2010-03-15 | 2011-04-29 | 410  |
|     16      | 2011-04-29 | 2011-11-01 | 186  |
|     89      | 2001-08-28 | 2002-05-03 | 248  |
|     89      | 2002-05-03 | 2003-09-15 | 500  |
+-------------+------------+------------+------+

列 days 是每个客户从一个订单到下一个订单的时间跨度(以天为单位)。

4

2 回答 2

0

你可以试试这个,很好的测试...

SELECT distinct(c1.customer_id) as 'customer_id', 

(select min(c2.order_date) from tmp2 c2 where c2.customer_id=c1.customer_id) as date1, (select max(c2.order_date) from tmp2 c2 where c2.customer_id=c1.customer_id) as date2, (DATEDIFF( (select max(c40.order_date) from tmp2 c40 where c40.customer_id=c1.customer_id) , (select min(c40.order_date) from tmp2 c40 where c40.customer_id=c1.customer_id) )) as 'days' FROMtmp2as c1 WHERE 1

于 2012-05-19T17:16:30.890 回答
0

你可以尝试这样的事情:

SELECT A.customer_id, 
    A.date as date1, 
    B.date as date2, 
    datediff(B.date, A.date) as days
FROM (select @curRowA := @curRowA + 1 AS row_number, 
        customer_id, 
        date from 
        orders 
    ORDER BY customer_id asc, date desc) A
INNER JOIN (select @curRowB := @curRowB + 1 AS row_number, 
            customer_id, 
            date from orders 
    ORDER BY customer_id asc, date desc offset 1) B
ON A.row_number = B.row_number AND A.customer_id = B.customer_id

如您所见,我有一个子查询,它从订单表中选择所有记录,但将 row_number 添加到 A,然后我做同样的事情,但只是向下移动一行(偏移量 1)并创建 B。然后我只是加入 A 和 B你得到结果。

A
    row_id, customer, date
    1, 1, d1
    2, 1, d2
    3, 1, d3
    4, 2, d4
    5, 2, d5

B
    row_id, customer, date
    1, 1, d2
    2, 1, d3
    3, 2, d4
    4, 2, d5

当 A 和 B 连接时

result
customer, date1, date2
1, d1, d2
1, d2, d3
2, d4, d5

更新:根据 Marc 的评论,最好尝试从相关查询(按客户)中获取 date2,在该查询中选择小于 date 的日期并将结果限制为 1 行。

于 2012-05-18T14:38:37.957 回答