I need to make an appointment, where I need to get the penultimate record, that is, the previous record and the date as well.
SELECT DISTINCT
prt_partner.razao_social,
prt_partner.end_cidade,
prt_partner.end_estado,
prt_partner_status.nome,
(SELECT MAX(_data_registro) FROM prt_partner_historic_status
WHERE prt_partner_historic_status.id_status = prt_partner.id_status) AS ultima_data,
(SELECT MAX(_data_registro) FROM prt_partner_historic_status
WHERE _data_registro NOT IN (SELECT MAX(_data_registro) FROM prt_partner_historic_status
WHERE prt_partner_historic_status.id_status = prt_partner.id_status)) AS data_anterior
from prt_partner
JOIN prt_partner_status ON (prt_partner.id_status = prt_partner_status.id_status)
WHERE prt_partner._ativo = 1;
I've been trying to do the select of the above form, but the "previous_date" returns me wrong
EDITINGTHEPOST
SELECTDISTINCTprt_partner.id_partnerasid_parceiro,prt_partner.razao_socialasnome_empresa,prt_partner.end_cidadeascidade,prt_partner.end_estadoasestado,prt_partner_status.nomeasstatus_atual,prt_partner_historic_status.dias_entre_statusasdias_entre_status,(SELECTMAX(_data_registro)FROMprt_partner_historic_statusWHEREprt_partner_historic_status.id_status=prt_partner.id_status)ASultima_data,(SELECT_data_registroFROMprt_partner_historic_statusWHEREprt_partner_historic_status.id_status=prt_partner.id_statusORDERBYid_historic_statusDESCLIMIT1OFFSET1)ASdata_anteriorFROMprt_partnerJOINprt_partner_statusON(prt_partner.id_status=prt_partner_status.id_status)JOINprt_partner_historic_statusON(prt_partner.id_partner=prt_partner_historic_status.id_partner)WHEREprt_partner._ativo=1ORDERBYid_parceiro;
Selectingthetopgivesmethefollowingreturn