MySQL lag column DATEDIFF -
suppose have following data:
customer_id contact_id comm_creation_day days_since_last_contact 10000 28036173295 2014-12-21 0 10000 28365672745 2015-01-29 datediff('2015-01-29','2014-12-21') 10000 28576719155 2015-02-26 datediff('2015-02-26','2014-01-29') 38409 28432217395 2015-02-07 0 38409 28565986955 2015-02-25 datediff('2015-02-25,'2015-02-07')
i'm trying days_since_last_contact
column, there's no lead
or lag
function in mysql. how do in single select
statement?
i tried following:
select customer_id, comm_id , comm_creation_day, previous_comm_creation_day ( select c.* , @prev previous_comm_creation_day , @prev := comm_creation_day contacts c, (select @prev:=null) vars order customer_id, c.comm_creation_day, c.comm_id ) sq order customer_id, comm_creation_day
but gave me:
customer_id contact_id comm_creation_day last_contact_date 10000 28036173295 2014-12-21 null 10000 28365672745 2015-01-29 2014-12-21 10000 28576719155 2015-02-26 2015-01-29 38409 28432217395 2015-02-07 2015-02-26 (this incorrect!) 38409 28565986955 2015-02-25 2015-02-07
where null
doesn't show last column in cases.
any ideas?
yes in mysql need use user defined variables such calculations as
select customer_id, contact_id, contact_date, days_since_last_contact from( select customer_id, contact_id, contact_date, @diff:= if(@prev_customer = customer_id,datediff(contact_date,@prev_date),0) days_since_last_contact, @prev_customer:=customer_id, @prev_date:= contact_date customer c,(select @prev_customer:=0,@prev_date:=null)x order customer_id,contact_date )x
Comments
Post a Comment