0
My select:
SELECT * (
SELECT v.placa FROM gr_veiculo v WHERE v.id = (
SELECT DISTINCT r.cavalo FROM lg_relacao_veiculo r
WHERE (
r.cavalo = (SELECT mv.gr_veiculo_id FROM mt_solicitacao_manutencao_veiculo mv WHERE mv.mt_solicitacao_manutencao_id = sm.id AND mv.ativo = TRUE ORDER BY id DESC LIMIT 1) OR
r.carreta1 = (SELECT mv.gr_veiculo_id FROM mt_solicitacao_manutencao_veiculo mv WHERE mv.mt_solicitacao_manutencao_id = sm.id AND mv.ativo = TRUE ORDER BY id DESC LIMIT 1) OR
r.carreta2 = (SELECT mv.gr_veiculo_id FROM mt_solicitacao_manutencao_veiculo mv WHERE mv.mt_solicitacao_manutencao_id = sm.id AND mv.ativo = TRUE ORDER BY id DESC LIMIT 1)
) and r.cavalo is not null
)
) AS placa,
sm.mt_solicitacao_manutencao_status_os_id AS status,
sm.atendimento_id
FROM mt_solicitacao_manutencao sm
INNER JOIN mt_solicitacao_manutencao_veiculo x
ON x.mt_solicitacao_manutencao_id = sm.id
WHERE sm.concessionaria_id = 15 and sm.ativo = true AND x.ativo = TRUE
Displays the following error:
SQL Error: ERROR: more than one row returned by a subquery used as an expression
What is the result of the query:
SELECT DISTINCT r.cavalo FROM lg_relacao_veiculo r 
WHERE (r.cavalo = (SELECT mv.gr_veiculo_id FROM mt_solicitacao_manutencao_veiculo mv WHERE mv.mt_solicitacao_manutencao_id = sm.id AND mv.ativo = TRUE ORDER BY id DESC LIMIT 1) OR
... OR
 r.carreta2 = (SELECT mv.gr_veiculo_id FROM mt_solicitacao_manutencao_veiculo mv WHERE mv.mt_solicitacao_manutencao_id = sm.id AND mv.ativo = TRUE ORDER BY id DESC LIMIT 1) 
 ) AND r.cavalo is not null

? A single horse? (Omitted part because it exceeded the size of the comment).– anonimo
What is happening is that your subquery is returning more than one record, in this part: WHERE v.id = ( SELECT DISTINCT r.cavalo FROM lg_relaca_veiculo r (...), it is as if you are trying to verify if 1 information is equal to a list of information (v.id = subquery that returns many things). What needs to be thought about is "should I only return a record?" if yes, Voce needs to adjust the subquery for this to occur.
– Elisiany Oliveira