2014-01-20 82 views
1

之間最大的區別,我有以下形式難MySQL查詢 - 獲取日期

account_id | call_date 
1   2013-06-07 
1   2013-06-09 
1   2013-06-21 
2   2012-05-01 
2   2012-05-02 
2   2012-05-06 

我想寫在call_date連續日期之間的一個MySQL查詢,將得到最大的差異(以天爲單位)的一個MySQL表爲每個account_id。因此,對於上面的例子,這個查詢的結果將是

account_id | max_diff 
1   12 
2   4 

我不知道如何做到這一點。這甚至有可能在MySQL查詢中執行?

我可以做datediff(max(call_date),min(call_date))但這會忽略第一次和最後一次通話日期之間的日期。我需要一些方法在每個account_id的每個連續call_date之間獲取datediff(),然後找到最大值。

+0

我不確定有可能通過單個查詢有效地完成此操作。 –

+1

你嘗試過什麼嗎?請閱讀[本文](http://whathaveyoutried.com)。最大差異是日期的最小值和最大值之間的差異,不是嗎?提示:使用集合函數'min()'和'max()',並使用'date_diff()'。 – Barranka

+0

請出示您的工作。 – Strawberry

回答

0
SELECT a1.account_id , max(a1.call_date - a2.call_date) 
FROM account a2, account a1 
WHERE a1.account_id = a2.account_id 
AND a1.call_date > a2.call_date 
AND NOT EXISTS 
    (SELECT 1 FROM account a3 WHERE a1.call_date > a3.call_date AND a2.call_date < a3.call_date) 
GROUP BY a1.account_id 

其中給出:

ACCOUNT_ID MAX(A1.CALL_DATE - A2.CALL_DATE) 
1   12 
2   4 
+0

我認爲@fancypants已經正確解釋了OP的要求 - 雖然我同意它有點含糊。 – Strawberry

+0

@Strawberry哦,我明白了.. –

+0

我的意思是SUCCESSIVE日期之間的最大差異。對不起,如果模糊。 – user1893354

1
CREATE TABLE t 
    (`account_id` int, `call_date` date) 
; 

INSERT INTO t 
    (`account_id`, `call_date`) 
VALUES 
    (1, '2013-06-07'), 
    (1, '2013-06-09'), 
    (1, '2013-06-21'), 
    (2, '2012-05-01'), 
    (2, '2012-05-02'), 
    (2, '2012-05-06') 
; 

select account_id, max(diff) from (
select 
account_id, 
timestampdiff(day, coalesce(@prev, call_date), call_date) diff, 
@prev := call_date 
from 
t 
, (select @prev:=null) v 
order by account_id, call_date 
) sq 
group by account_id 

| ACCOUNT_ID | MAX(DIFF) | 
|------------|-----------| 
|   1 |  12 | 
|   2 |   4 | 
+0

我收到消息'Function'set_user_var'只能用在最外層的select或order by子句中,不能和聚合函數一起使用。' – user1893354

+0

這是而不是直接從MySQL發出的錯誤消息,但我敢打賭,你可以在開始查詢之前發出'set @prev = null;'來避開它,並且在'from'之後移除',(選擇@prev:= null)v'在選擇。 – fancyPants

+0

這是我使用的數據庫的問題嗎? – user1893354

0

帶電作業如果您有account_id, call_date索引,那麼你可以相當有效地做到這一點無變量:

select account_id, max(call_date - prev_call_date) as diff 
from (select t.*, 
      (select t2.call_date 
       from table t2 
       where t2.account_id = t.account_id and t2.call_date < t.call_date 
       order by t2.call_date desc 
       limit 1 
      ) as prev_call_date 
     from table t 
    ) t 
group by account_id; 
2

我敢肯定,FP的答案會更快,但只是爲了好玩...

SELECT account_id 
    , MAX(diff) max_diff 
    FROM 
    (SELECT x.account_id 
      , DATEDIFF(MIN(y.call_date),x.call_date) diff 
     FROM my_table x 
     JOIN my_table y 
      ON y.account_id = x.account_id 
      AND y.call_date > x.call_date 
     GROUP 
      BY x.account_id 
      , x.call_date 
    ) z 
GROUP 
    BY account_id; 
0

只是爲了教育目的,與JOIN做:

SELECT t1.account_id, 
    MAX(DATEDIFF(t2.call_date, t1.call_date)) AS max_diff 
FROM t t1 
LEFT JOIN t t2 
ON t2.account_id = t1.account_id 
    AND t2.call_date > t1.call_date 
LEFT JOIN t t3 
ON t3.account_id = t1.account_id 
    AND t3.call_date > t1.call_date 
    AND t3.call_date < t2.call_date 
WHERE t3.account_id IS NULL 
GROUP BY t1.account_id 

既然你沒沒有指定,這顯示max_diffNULL帳戶只有1個電話。