SQL - IN inside a SUB SELECT with NOT IN - ORACLE

Asked

Viewed 640 times

1

I wanted to make my INSERT dynamic, I don’t need to change the ids manually.

I have this script that returns all PRODUCTS that does not have 83 relationship with the EST_PROD_PRECO_CUSTO_EMPRESA table

SELECT prod.ID_PRODUTO AS "ID DO PRODUTO" FROM ERP.EST_PRODUTO prod
LEFT JOIN ERP.EST_PROD_PRECO_CUSTO_EMPRESA prodEmp ON prodEmp.ID_PRODUTO = 
prod.ID_PRODUTO
GROUP BY  prod.ID_PRODUTO
HAVING COUNT(prodEmp.ID_PRODUTO) !=83 

My return and 22 data (ID_PRODUTO), done this need another script to return companies that are not related to these ID_PRODUTO in the EST_PROD_PRECO_CUSTO_EMPRESA table. for me to create an Insert and make that relationship.

SELECT DISTINCT emp.ID_EMPRESA FROM ERP.CF_EMPRESA emp
WHERE EMP.ID_EMPRESA NOT IN (select emp.id_empresa FROM
ERP.EST_PROD_PRECO_CUSTO_EMPRESA prd
LEFT JOIN ERP.EST_PRODUTO prod ON prod.ID_PRODUTO = prd.ID_PRODUTO
INNER JOIN ERP.CF_EMPRESA emp ON prd.ID_EMPRESA = emp.ID_EMPRESA
WHERE prod.ID_PRODUTO = 127011575) /*id que consegui no script a cima*/

With this return of ID_EMPRESA I create my INSERT. The id I use Quence with a Trigger

INSERT INTO EST_PROD_PRECO_CUSTO_EMPRESA ( ID_PRODUTO, ID_EMPRESA,  
    CUSTO_OPERACIONAL, OUTRAS_DESPESAS)
VALUES (127011575, 793, 0, 0)

But when I try to join the sql I have no precise result because there is product that has no relationship with company X, but the other product has no relationship with company Y but has with company X that the other product did not have.

SELECT DISTINCT emp.ID_EMPRESA FROM ERP.CF_EMPRESA emp
WHERE EMP.ID_EMPRESA NOT IN 
    (SELECT emp.id_empresa FROM
    ERP.EST_PROD_PRECO_CUSTO_EMPRESA prd
    LEFT JOIN ERP.EST_PRODUTO prod ON prod.ID_PRODUTO = prd.ID_PRODUTO
    INNER JOIN ERP.CF_EMPRESA emp ON prd.ID_EMPRESA = emp.ID_EMPRESA
    WHERE prod.ID_PRODUTO  IN(
     SELECT prod.ID_PRODUTO AS "ID DO PRODUTO" FROM ERP.EST_PRODUTO prod
     LEFT JOIN ERP.EST_PROD_PRECO_CUSTO_EMPRESA prodEmp ON
         prodEmp.ID_PRODUTO = prod.ID_PRODUTO
     GROUP BY  prod.ID_PRODUTO
     HAVING COUNT(prodEmp.ID_PRODUTO) !=83 ))

I do not know how to treat each ID_PRODUTO separately from the list until the end of it, my NOT IN does not know differentiates each sub SELECT. Someone knows a solution even with PL/SQL?

  • wouldn’t be ... WHERE (Prod.ID_PRODUTO ,Prod.ID_EMPRESA) IN ( SELECT Prod.ID_PRODUTO , Prod.ID_EMPRESA ? Double key ...

  • I’ll try, I didn’t know of this possibility of double key.

  • I got the same result that if I had used EXISTS, he brought me a company that I do not link any with the EST_PROD_PRECO_CUSTO_EMPRESA table , and how my case would need to bring companies that were linked to the product but n had link in the EST_PROD_PRECO_CUSTO_EMPRESA table because this table contains the Product and Company ID. I appealed to the PL/SQL and I think it will give me the result I need.

  • You need to know the structure of tables and relationships so that someone can give their opinion.

  • The relationship is very simple, I have EST_PRODUTO, CF_EMPRESA each with their due variables, ai tenho EST_PRODUTO_EMPRESA, QUE possui ID de cada uma, e tenho EST_PROD_PRECO_CUSTO_EMPRESA que também possui o id de cada, e uns double adicionais.

  • I believe you need to do the rel. emp and product for each Join , so I understood the product x differs from empres 1 for company 2, correct :?

  • correct, type with PL/SQL already understood how to do, but using SQL did not see a solution

  • By chance this post with its answer, 2010 helped me in PL/SQL tbm kkk [https://forum.imasters.com.br/topic/411769-resolvido-resultado-de-select-dentro-de-umavariable/]

  • This post deserved a tidy , a conclusion.

  • @Motta blz? At the time I solved my problem with PL/SQL because I needed to Insert with data from two other tables.

Show 5 more comments

1 answer

0

[CONCLUSION] Using PL/SQL

random_uuid() and a Function of mine that generates UUID values.

DECLARE 

FOR V_FUNC IN
    (SELECT  prod.ID_PRODUTO FROM ERP.EST_PRODUTO prod
                INNER JOIN ERP.EST_PROD_PRECO_CUSTO_EMPRESA prodEmp on prodEmp.ID_PRODUTO = prod.ID_PRODUTO
                GROUP BY  prod.ID_PRODUTO
                HAVING COUNT(prodEmp.ID_PRODUTO) !=83) 
LOOP

            FOR V_FUNC_EMPRESA IN (SELECT DISTINCT emp.ID_EMPRESA FROM ERP.CF_EMPRESA emp
                                            WHERE EMP.ID_EMPRESA NOT IN (select emp.id_empresa FROM ERP.EST_PROD_PRECO_CUSTO_EMPRESA prd
                                            INNER JOIN ERP.EST_PRODUTO prod ON prod.ID_PRODUTO = prd.ID_PRODUTO
                                            INNER JOIN ERP.CF_EMPRESA emp ON prd.ID_EMPRESA = emp.ID_EMPRESA
                                            WHERE prod.ID_PRODUTO = V_FUNC.ID_PRODUTO)
                                            AND EMP.ID_EMPRESA != 110)

                    LOOP
                         INSERT

                                        INSERT 
                                                    INTO EST_PROD_PRECO_CUSTO_EMPRESA 
                                                            (
                                                                                                    ID_PRODUTO, 
                                                             ID_EMPRESA, CUSTO_OPERACIONAL,
                                                                                         OUTRAS_DESPESAS, 
                                                                    PERCENTUAL_OUTRAS_DESPESAS, 
                                                                                                 PRECO_CUSTO, 
                                                                                     PRECO_CUSTO_FINAL, 
                                                                                     PRECO_CUSTO_MEDIO, 
                                                                                    OTAL_OUTROS_CUSTOS,
                                                                OUTROS_CUSTOS_ULTIMA_ENTRADA,
                                                                                                                UUID, 
                                                                                DATA_ULTIMO_REAJUSTE
                                                            )
                                            VALUES
                                                (
                                                                V_FUNC.ID_PRODUTO, 
                                                                V_FUNC_EMPRESA.ID_EMPRESA, 
                                                                        0,
                                                                        0, 
                                                                        0,
                                                                        0, 
                                                                        0,
                                                                        0,
                                                                        0, 
                                                                        0, 
                                                random_uuid(),
                                                            SYSDATE
                                                )

                                ;

            END LOOP;

END LOOP;

END;
  • Is that the answer or something that complements the doubt? if the answer is the second option, you’d better edit the question than add the text as an answer ;)

  • is a PL/SQL preview but wanted to know if it was possible to do using SQL only.

Browser other questions tagged

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