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 ...
– Motta
I’ll try, I didn’t know of this possibility of double key.
– Guilherme Oliveira
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.
– Guilherme Oliveira
You need to know the structure of tables and relationships so that someone can give their opinion.
– Motta
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.
– Guilherme Oliveira
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 :?
– Motta
correct, type with PL/SQL already understood how to do, but using SQL did not see a solution
– Guilherme Oliveira
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/]
– Guilherme Oliveira
This post deserved a tidy , a conclusion.
– Motta
@Motta blz? At the time I solved my problem with PL/SQL because I needed to Insert with data from two other tables.
– Guilherme Oliveira