2012-02-23 43 views
2

我試圖找出需要多長時間直到客戶再次訂購我的eshop。日期與不同產出的差異

列表的名稱eshop_flat_sales_order

customer_email created_at status 
------------------------------------ 
a(at)a.com  12.1.10 complete 
b(at)a.com  14.2.10 cancelled 
c(at)a.com  16.1.10 complete 
a(at)a.com  18.1.10 complete 
c(at)a.com  18.1.10 complete 
b(at)a.com  20.1.10 complete 

與查詢

SELECT * 
FROM eshop_flat_sales_order 
ORDER BY customer_email 

生病讓所有訂單的日期的電子郵件。就像這樣:

customer_email created_at status 
------------------------------------ 
a(at)a.com  12.1.10 complete 
a(at)a.com  18.1.10 complete 
b(at)a.com  14.2.10 cancelled 
b(at)a.com  20.1.10 complete 
c(at)a.com  16.1.10 complete 
c(at)a.com  18.1.10 complete 

現在,這將是巨大拿到查詢還告訴我,過了多長時間一(在)a.com再次訂購。在這個例子中,這將是6天。對於c(at)a.com這將是2天。那麼到底我需​​要所有這些日期的平均水平,但我要管理該:)

感謝這麼多的答案

+0

日期的格式是什麼?我有點希望'DATE'?還有一個存儲'customer_email's的中央表嗎? – 2012-02-23 13:03:09

回答

0

爲了得到所要求的結果,請嘗試以下操作:

SELECT 
    customer_email, 
    DATEDIFF(MAX(date), MIN(date)) 
FROM eshop_flat_sales_order 
GROUP BY customer_email 
+0

這會發現第一個和最後一個訂單之間的差距,而不是按照時間順序排列的訂單之間的差距。另外,沒有重新排序的用戶和在同一天重新排序的用戶之間沒有區別(如在兩種情況下,DATEDIFF(MAX,MIN)將爲0) – 2012-02-23 13:27:57

+0

以按時間順序獲得訂單之間的差距,您可能必須將「ORDER BY created_at」添加到查詢的末尾 – 2012-02-23 13:32:19

+0

並非如此,因爲它仍會將其組合在唯一的customer_email上。我能想象的唯一方法就是加入一個連接,例如't1.date 2012-02-23 13:34:49

1

這裏有一個查詢返回的最新差距,以天爲單位,爲每一位客戶,使用JOIN:

SELECT e1.customer_email, DATEDIFF(e1.created_at, e2.created_at) AS gap 
FROM eshop_flat_sales_order e1 
LEFT JOIN eshop_flat_sales_order e2 
    ON e2.customer_email = e1.customer_email 
    AND e2.created_at < e1.created_at 
LEFT JOIN eshop_flat_sales_order e3 
    ON e3.customer_email = e1.customer_email 
    AND e3.created_at < e2.created_at 
WHERE e3.customer_email IS NULL 
ORDER BY e1.customer_email 

這個查詢假設created_atDATE場。

它將返回NULL對於gap其中一個客戶只有一個訂單。如果您不想僅向一個訂單返回結果,請將第一個連接從LEFT JOIN更改爲JOIN

這裏的另一個版本,只考慮complete訂單:

SELECT e1.customer_email, DATEDIFF(e1.created_at, e2.created_at) AS gap 
FROM eshop_flat_sales_order e1 
LEFT JOIN eshop_flat_sales_order e2 
    ON e2.customer_email = e1.customer_email 
    AND e2.created_at < e1.created_at 
    AND e2.status = 'complete' 
LEFT JOIN eshop_flat_sales_order e3 
    ON e3.customer_email = e1.customer_email 
    AND e3.created_at < e2.created_at 
    AND e3.status = 'complete' 
WHERE e1.status = 'complete' 
    AND e3.customer_email IS NULL 
ORDER BY e1.customer_email 

這將顯示所有所有相應的訂單之間的差距,爲所有的客戶:

SELECT e1.customer_email, DATEDIFF(e1.created_at, e2.created_at) AS gap 
FROM eshop_flat_sales_order e1 
JOIN eshop_flat_sales_order e2 
    ON e2.customer_email = e1.customer_email 
    AND e2.created_at < e1.created_at 
    AND e2.status = 'complete' 
LEFT JOIN eshop_flat_sales_order e3 
    ON e3.customer_email = e1.customer_email 
    AND e3.created_at < e1.created_at 
    AND e3.created_at > e2.created_at 
    AND e3.status = 'complete' 
WHERE e1.status = 'complete' 
    AND e3.customer_email IS NULL 
ORDER BY e1.customer_email ASC, e1.created_at DESC 

這將顯示的平均差距對於每位顧客:

SELECT e1.customer_email, AVG(DATEDIFF(e1.created_at, e2.created_at)) AS gap 
FROM eshop_flat_sales_order e1 
JOIN eshop_flat_sales_order e2 
    ON e2.customer_email = e1.customer_email 
    AND e2.created_at < e1.created_at 
    AND e2.status = 'complete' 
LEFT JOIN eshop_flat_sales_order e3 
    ON e3.customer_email = e1.customer_email 
    AND e3.created_at < e1.created_at 
    AND e3.created_at > e2.created_at 
    AND e3.status = 'complete' 
WHERE e1.status = 'complete' 
    AND e3.customer_email IS NULL 
GROUP BY e1.customer_email 
+0

謝謝!所以這意味着如果有客戶假設其a.com(a.com)有3個訂單,則需要第二個和第三個訂單? – user1228353 2012-02-23 14:38:10

+0

是的,總是最近兩個訂單之間的差距。 – 2012-02-23 14:40:30

+0

好,謝謝;並且有沒有辦法看到所有訂單的差距,可能是增加了另一行? – user1228353 2012-02-23 14:41:22

0

編輯: 此查詢將爲您提供客戶,最後訂單和以前的訂單。 檢查它是否反映您的需求。如果是的話,只需在列上做個日期:

with last_order as 
(
    select customer_email, max(created_at) as max_order, max (ID) as max_id 
    from eshop_flat_sales_order 
    where status='complete' 
    group by customer_email 
) 
Select customer_email, MAX_ORDER, 
(select top 1 created_at 
from eshop_flat_sales_order o 
where customer_email=lo.customer_email and ID!= lo.max_id and status='complete' 
order by created_at desc) as PREVIOUS_ORDER 
from last_order LO 

您是否每個客戶都有2條記錄?如果是的話,試試這個:

select customer_email, datediff(day, min(created_at),max(created_at)) 
from eshop_flat_sales_order 
where status='complete' 
group by customer_email 
having count(*)=2 
+0

謝謝大家。有客戶有7個訂單,有2個訂單的客戶,只有一個訂單的客戶。 – user1228353 2012-02-23 14:37:07

0

你很可能是最好用SQL變量做...讓你隨時可以使用任何的最後一個人是和活動及其相應的日期......

SELECT 
     PreSort.customer_email, 
     @lastDate as LastOrderDate, 
     PreSort.Created_At, 
     if(@lastCust = PreSort.customer_email, 
     datediff(@lastDate, PreSort.Created_At), null) as DaysDiff, 
     @lastDate := PreSort.Created_At as PreserveDate, 
     @lastCust := PreSort.customer_email as PreserveCustomer 
    FROM 
     (select efso.customer_email, 
       efso.created_at 
      from eshop_flat_sales_order AS efso 
      where efso.status = 'complete' 
      order by efso.customer_email, 
        efso.created_at) PreSort, 
     (SELECT @lastCust := '', @lastDate := null) AS SqlVars 

現在,您正在尋找最後一次實際訂單...因此,您只需訪問您的網站並嘗試下訂單即可獲得「完整」或「取消」狀態。

應該創建一個結果遊標類似

customer_email LastOrderDate created_at DaysDiff PreserveDate PreserveCustomer 
-------------- ------------- ---------- ---------- ------------ ---------------- 
a(at)a.com  NULL   12.1.10 NULL  12.1.10  a(at)a.com 
a(at)a.com  12.1.10  18.1.10 6   18.1.10  a(at)a.com 
b(at)a.com  NULL   20.1.10 NULL  20.1.10  b(at)a.com 
c(at)a.com  NULL   16.1.10 NULL  16.1.10  c(at)a.com 
c(at)a.com  16.1.10  18.1.10 2   18.1.10  c(at)a.com 

現在,如果你想平均數,你可以滾這些記錄並從DaysDiff得到的平均水平。但是,您可能希望應用WHERE刪除表示第一個訂單的「NULL」條目,並將那些具有HAD第二個訂單的條目留下。您甚至可以爲第一個日期,最後一個日期,總訂單應用GROUPING值。