Inner Join in 5 tables

Asked

Viewed 130 times

0

I have the tables:

  • reparable
  • projects
  • contracts
  • itens_contracted
  • controle_contracts

I made that Consultation:

Select
    a.qtd as quantidade_contratada,
    b.pn,
    c.numero_contrato,
    count(d.id_pn) qtd_executada,
    (a.qtd - count(d.id_pn)) saldo,
    e.projeto
FROM
    itens_contratados a
    LEFT JOIN reparaveis b ON a.id_pn = b.id_pn
    INNER JOIN contratos c ON a.id_contrato = c.id_contrato
    INNER JOIN controle_contratos d ON a.id_pn = d.id_pn
    INNER JOIN projetos e ON e.id_projeto = d.id_projeto and e.id_projeto = a.id_projeto
Where
    d.status <> 'Cancelada' and d.status <> 'Aguardando Cancelamento'
GROUP BY 
    b.pn, e.projeto DESC

I have as a result what I need when there is record in the table controle_contratos, but I also need to have the b.id_pn (b.pn of Group By) that do not appear in the table controle_contratos, someone knows where I’m going wrong?

  • What is the relationship between the tables? Could post the MER?

  • Djalma reparaveis tem o id_pn e pn projeto id_projeto e projeto contratos id_contrato e numero_contrato a tabela itens_contratados tem id_pn id_projeto e id_contrato e a ultima tem id_pn, id_contrato, id_projeto ficou confuso ficou confuso..... I don’t know how to show the MER around here

  • I want to appear the items that have no record in the table of left Join, but do not appear... I want all itens_contracted, even if it has not related in tebela controle_contracts

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.