3
I need to make a query, where I need to get the penultimate record, IE, the previous record and the date also.
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 select the way above, only the "previous data_me" returns wrong
EDITING THE 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;
This with uses at the very beginning?? here accused as error
– gabrielfalieri
You are using which SGDB?
– Ismael
I am using mysql
– gabrielfalieri
This information was missing in your question because WITH is not supported in mysql. I will include a solution for mysql in the answer as soon as possible.
– Ismael
Thank you very much
– gabrielfalieri
@gabrielfalieri thinking of a palliative solution for
MySQL
, I think in place ofWITH
, you could create a view, and in place ofrow_number
, use the@rank:=@rank+1
.– Ismael
Is that this select will become a view, the view can call another view?
– gabrielfalieri
Absolutely. At the end of the process, you will have two views.
– Ismael
I have no idea how to do this... with select simple, instead of view would not work?
– gabrielfalieri
As you will create a view from this select, then you cannot have two selects in the same batch. What you would do is separate for a view the first part (id_status, data_record and line numbering) and from the main query, search only the record MAX - 1 of this new view.
– Ismael
I edited my sql now, it looks like it’s
– gabrielfalieri
With LIMIT and OFFSET solved?
– Ismael
Not resolved yet
– gabrielfalieri
Use a variable in OFFSET to return max()-1. I can’t test now, but look at this example
SELECT max(compoid)-1, t.* FROM Tabela t ORDER BY compoid DESC LIMIT 1;
– Ismael
Even though the id is not sequential? Type, for example, in select not a proper order?
– gabrielfalieri
It is that the idea of max()-1 does not refer to the id of the table, but rather, the line in question. Creating a new column with the line number, we could take the second-to-last line with the offset. You can try with id_status < that max(id_status) too.
– Ismael
Hello @gabrielfalieri, consider accepting my answer if it has been useful to you. If you think she’s incomplete or doesn’t respond to you, make the appropriate comments so I can improve her.
– Ismael