SELECT destinatario.contactid as destinocontact,CONCAT('Creado hace ',datediff(DATE(NOW()),DATE(y.biz_dateadd)),' dias') as creadoHace,CONCAT(creator.firstname,' ', creator.lastname) as creador ,CONCAT(destinatario.firstname,' ',destinatario.lastname) as destino, CONCAT(empresa.accountname) as empresa, Y.*
from (
SELECT destinatario.contactid,MAX(n.notesid) as notesid
from vtiger_notes as n
inner join vtiger_contactdetails as creator on creator.contactid=n.biz_contact
inner join vtiger_senotesrel as sn on sn.notesid=n.notesid
inner join vtiger_contactdetails as destinatario on destinatario.contactid=sn.crmid
where creator.accountid=717 and creator.isComercial='1'
and n.biz_type<>'Creacion contacto' and n.biz_contact = 644075
group by destinatario.contactid
) as x inner join vtiger_notes as y on x.notesid = y.notesid
inner join vtiger_contactdetails as creator on creator.contactid=y.biz_contact
inner join vtiger_senotesrel as sn on sn.notesid=y.notesid
inner join vtiger_contactdetails as destinatario on destinatario.contactid=sn.crmid
inner join vtiger_account as empresa on empresa.accountid=destinatario.accountid
where creator.accountid= 717 and creator.isComercial='1'
and y.biz_type<>'No tomará el servicio'
and y.biz_estado ='1'
and y.biz_type<>'Creacion contacto' and datediff(DATE(NOW()),DATE(y.biz_dateadd)) > 7
SELECT destinatario.contactid as destinocontact,CONCAT('Creado hace ',datediff(DATE(NOW()),DATE(y.biz_dateadd)),' dias') as creadoHace,CONCAT(creator.firstname,' ', creator.lastname) as creador ,CONCAT(destinatario.firstname,' ',destinatario.lastname) as destino, CONCAT(empresa.accountname) as empresa, Y.*
from (
SELECT destinatario.contactid,MAX(n.notesid) as notesid
from vtiger_notes as n
inner join vtiger_contactdetails as creator on creator.contactid=n.biz_contact
inner join vtiger_senotesrel as sn on sn.notesid=n.notesid
inner join vtiger_contactdetails as destinatario on destinatario.contactid=sn.crmid
where creator.accountid=717 and creator.isComercial='1'
and n.biz_type<>'Creacion contacto' and n.biz_contact = 644075
group by destinatario.contactid
) as x inner join vtiger_notes as y on x.notesid = y.notesid
inner join vtiger_contactdetails as creator on creator.contactid=y.biz_contact
inner join vtiger_senotesrel as sn on sn.notesid=y.notesid
inner join vtiger_contactdetails as destinatario on destinatario.contactid=sn.crmid
inner join vtiger_account as empresa on empresa.accountid=destinatario.accountid
where creator.accountid= 717 and creator.isComercial='1'
and y.biz_type<>'No tomará el servicio'
and y.biz_estado ='1'
and y.biz_type<>'Creacion contacto' and datediff(DATE(NOW()),DATE(y.biz_dateadd)) > 7