Eu estou precisando fazer uma consulta, onde preciso pegar o penúltimo registro, ou seja, o registro anterior e a data também.
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;
Venho tentando fazer o select da forma acima, só que o "data_anterior" me retorna errado
EDITANDO O POST
SELECT DISTINCT
prt_partner.id_partner as id_parceiro,
prt_partner.razao_social as nome_empresa,
prt_partner.end_cidade as cidade,
prt_partner.end_estado as estado,
prt_partner_status.nome as status_atual,
prt_partner_historic_status.dias_entre_status as dias_entre_status,
(SELECT MAX(_data_registro) FROM prt_partner_historic_status
WHERE prt_partner_historic_status.id_status = prt_partner.id_status) AS ultima_data,
(SELECT _data_registro FROM prt_partner_historic_status
WHERE prt_partner_historic_status.id_status = prt_partner.id_status ORDER BY id_historic_status DESC LIMIT 1 OFFSET 1) AS data_anterior
FROM prt_partner
JOIN prt_partner_status ON (prt_partner.id_status = prt_partner_status.id_status)
JOIN prt_partner_historic_status ON (prt_partner.id_partner = prt_partner_historic_status.id_partner)
WHERE prt_partner._ativo = 1 ORDER BY id_parceiro;