Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Ericsson Berg

Dois selects trazendo dados específicos

Recommended Posts

Tenho dua tabelas A e B, e preciso que ocorra o seguinte:

 

Através do primeiro select, o segundo faça uma pesquisa específica com o campo COD_EMRPESA e traga os dados.

 

Select A

SELECT EMPRESAS.COD_EMPRESA COD_EMP, 
                   EMPRESAS.NOME EMPRESA, 
                   SUM((ESTOQUE.QTDE)*(ITENS_CUSTOS.CUSTO_CONTABIL)) VLR_ESTOQUE
            
          FROM  ".$schema.".ITENS_FORNECEDOR, 
                ".$schema.".ITENS_CUSTOS ITENS_CUSTOS, 
                ".$schema.".ESTOQUE, 
                ".$schema.".EMPRESAS
          WHERE ITENS_CUSTOS.COD_ITEM = ITENS_FORNECEDOR.COD_ITEM
                  AND ITENS_CUSTOS.COD_FORNECEDOR = ITENS_FORNECEDOR.COD_FORNECEDOR
                  AND ESTOQUE.COD_ITEM = ITENS_CUSTOS.COD_ITEM
                  AND ESTOQUE.COD_FORNECEDOR = ITENS_CUSTOS.COD_FORNECEDOR
                  AND ESTOQUE.COD_EMPRESA = ITENS_CUSTOS.COD_EMPRESA
                  AND EMPRESAS.COD_EMPRESA = ESTOQUE.COD_EMPRESA
                  AND ESTOQUE.QTDE > 0
            GROUP BY EMPRESAS.COD_EMPRESA, EMPRESAS.NOME
            ORDER BY EMPRESAS.NOME

Select B

SELECT EMPRESAS.COD_EMPRESA COD_EMP,
             EMPRESAS.NOME, 
             SUM(ESTOQUE.QTDE * ITENS_CUSTOS.CUSTO_CONTABIL)  VLR_OBSOLETO
FROM    ".$schema.".ITENS, 
        ".$schema.".ITENS_CUSTOS,
        ".$schema.".ITENS_FORNECEDOR, 
        ".$schema.".ESTOQUE, 
        ".$schema.".EMPRESAS, 
        ".$schema.".FORNECEDOR_ESTOQUE,
        ".$schema.".ITENS_GRUPO_INTERNO,
        ".$schema.".ITENS_SUB_GRUPO
WHERE  ITENS_CUSTOS.COD_ITEM = ITENS.COD_ITEM
       AND ITENS_CUSTOS.COD_ITEM = ITENS_FORNECEDOR.COD_ITEM
       AND ITENS_CUSTOS.COD_FORNECEDOR = ITENS_FORNECEDOR.COD_FORNECEDOR
       AND ITENS_CUSTOS.COD_EMPRESA = EMPRESAS.COD_EMPRESA
       AND ITENS_FORNECEDOR.COD_ITEM = ITENS.COD_ITEM
       AND ESTOQUE.COD_ITEM = ITENS_CUSTOS.COD_ITEM
       AND ESTOQUE.COD_FORNECEDOR = ITENS_CUSTOS.COD_FORNECEDOR
       AND ESTOQUE.COD_EMPRESA = ITENS_CUSTOS.COD_EMPRESA
       AND EMPRESAS.COD_EMPRESA = ESTOQUE.COD_EMPRESA
       AND EMPRESAS.COD_EMPRESA = ITENS_CUSTOS.COD_EMPRESA
       AND FORNECEDOR_ESTOQUE.COD_FORNECEDOR = ITENS_CUSTOS.COD_FORNECEDOR
       AND FORNECEDOR_ESTOQUE.COD_FORNECEDOR = ITENS_FORNECEDOR.COD_FORNECEDOR
       AND ITENS_GRUPO_INTERNO.COD_GRUPO_INTERNO = ITENS.COD_GRUPO_INTERNO
       AND ITENS_GRUPO_INTERNO.COD_GRUPO_INTERNO = ITENS_SUB_GRUPO.COD_GRUPO_INTERNO
       AND ITENS_SUB_GRUPO.COD_SUB_GRUPO_INTERNO = ITENS.COD_SUB_GRUPO_INTERNO
       AND ITENS_SUB_GRUPO.COD_GRUPO_INTERNO = ITENS_GRUPO_INTERNO.COD_GRUPO_INTERNO
       AND (ESTOQUE.QTDE > 0 
       AND ((trunc(sysdate-ITENS_CUSTOS.ultima_venda) >= 0 
       AND  trunc(sysdate-itens_custos.ultima_compra) >= '0') 
       OR  (ITENS_CUSTOS.ULTIMA_VENDA IS NULL  
       AND  ITENS_CUSTOS.ULTIMA_COMPRA IS NULL )
       OR  (trunc(sysdate-itens_custos.ultima_compra) >= '0' 
       AND  ITENS_CUSTOS.ULTIMA_VENDA IS NULL  )
       OR ( trunc(sysdate-ITENS_CUSTOS.ultima_venda) >= 0 
       AND  ITENS_CUSTOS.ULTIMA_COMPRA IS NULL ))
       AND  FORNECEDOR_ESTOQUE.NOME_FORNECEDOR NOT LIKE '%Outro%'  )

GROUP BY EMPRESAS.COD_EMPRESA, EMPRESAS.NOME
ORDER BY EMPRESAS.NOME

E estes dados sejam carregados via PHP.

 

Alguma dica????

Compartilhar este post


Link para o post
Compartilhar em outros sites

Olá, pelo que entendi você quer pegar um dado com o primeiro select e pesquisar com ele no segundo né?

 

Não sei se é a melhor forma mas pode salvar o dado do primeiro select numa variável e pesquisar com esta variável no segundo select

Compartilhar este post


Link para o post
Compartilhar em outros sites

Também não sei se é o melhor mais foi assim que fiz.

 

Neste código completo utilizei 4 selects em sequência para obter todos os dados, e funcionou corretamente, mas gostaria de saber se tem algum modo unindo os selects pelo UNION ALL ou INNER JOIN.

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.