Fórum Select em 3 tabelas e update em 1 delas #456288
23/09/2013
0
Olá,
Sou iniciante em Oracle estou encontrando dificuldades em realizar um rotina em meu trabalho.
Segue minha duvida,
Tenho 3 tabelas
Tab1
codigo_item, posicao_fiscal, pis,cofins
tab2
codigo_item, pis, cofins,nota_fiscal
tab3
nota_fiscal, data_documento
Pergunta, através do código NCM(tab1) preciso de determinadas notas fiscais(tab2) em uma data selecionada(tab3). após o resultado preciso alterar as colunas pis e cofins da tab2,com um determinado valor.
Fiz o select, porém não estou conseguindo fazer o UPDATE, alguém poderia me ajudar?
Segue o código do select
select A.Item_Estoque, C.Numero_Nota_Fiscal, B.Quantidade, A.Base_Pis, A.Base_Cofins, C.Dta_Entrada_Saida from pec_item_estoque A, fat_movimento_item B, fat_movimento_capa C
where posicao_fiscal = 40169990
and C.dta_entrada_saida between to_date('01/08/2013','dd/mm/yyyy')
and to_date('02/08/2013','dd/mm/yyyy')
and C.revenda = 1
and A.item_estoque = B.Item_Estoque
and B.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL
and C.Tipo_Transacao = 'P01'
order by C.Numero_Nota_Fiscal
Sou iniciante em Oracle estou encontrando dificuldades em realizar um rotina em meu trabalho.
Segue minha duvida,
Tenho 3 tabelas
Tab1
codigo_item, posicao_fiscal, pis,cofins
tab2
codigo_item, pis, cofins,nota_fiscal
tab3
nota_fiscal, data_documento
Pergunta, através do código NCM(tab1) preciso de determinadas notas fiscais(tab2) em uma data selecionada(tab3). após o resultado preciso alterar as colunas pis e cofins da tab2,com um determinado valor.
Fiz o select, porém não estou conseguindo fazer o UPDATE, alguém poderia me ajudar?
Segue o código do select
select A.Item_Estoque, C.Numero_Nota_Fiscal, B.Quantidade, A.Base_Pis, A.Base_Cofins, C.Dta_Entrada_Saida from pec_item_estoque A, fat_movimento_item B, fat_movimento_capa C
where posicao_fiscal = 40169990
and C.dta_entrada_saida between to_date('01/08/2013','dd/mm/yyyy')
and to_date('02/08/2013','dd/mm/yyyy')
and C.revenda = 1
and A.item_estoque = B.Item_Estoque
and B.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL
and C.Tipo_Transacao = 'P01'
order by C.Numero_Nota_Fiscal
Paulo
Curtir tópico
+ 0
Responder
Posts
04/10/2013
Mariana Carvalho
Paulo, ainda com o problema?
Responder
Gostei + 0
06/10/2013
Paulo
Olá Mariana,
Então já consegui efetuar esse select, com 3 tabelas. Segue o código abaixo.
UPDATE PEC_ITEM_ESTOQUE A
SET A.BASE_PIS = '0,00'
WHERE A.POSICAO_FISCAL = 40169990
AND EXISTS (
SELECT 1
FROM FAT_MOVIMENTO_ITEM B
INNER JOIN FAT_MOVIMENTO_CAPA C ON C.NUMERO_NOTA_FISCAL = B.NUMERO_NOTA_FISCAL
WHERE A.ITEM_ESTOQUE = B.ITEM_ESTOQUE
AND C.DTA_ENTRADA_SAIDA BETWEEN TO_DATE('12/08/2013', 'DD/MM/YYYY') AND TO_DATE('12/08/2013', 'DD/MM/YYYY')
AND C.REVENDA = 1
AND C.TIPO_TRANSACAO = 'P01'
)
Agora estou com dificuldades para duas tabelas.
EXEMPLO:
FAT_MOVIMENTO_ITEM E FAT_MOVIMENTO_CAPA O ÚNICO CAMPO EM COMUM NAS DUAS TABELAS É O NUMERO_NOTA_FISCAL
SELECT *
FROM FAT_MOVIMENTO_ITEM A
WHERE A.ITEM_ESTOQUE = 31025
AND EXISTS ( select A.Item_Estoque from fat_movimento_capa C
where C.dta_entrada_saida between to_date('12/08/2013','dd/mm/yyyy')
and to_date('12/08/2013','dd/mm/yyyy')
and C.Status = 'F'
and C.revenda = 1
and A.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL
and C.Tipo_Transacao = 'P01'
)
PORÉM O ÚNICO CRITÉRIO QUE ELE ESTÁ ACEITANDO É ESSE "and A.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL" , OUTROS COMO "and C.Status = 'F'" , 'and C.Tipo_Transacao = 'P01'" , "and C.revenda = 1" ELE ESTÁ IGNORANDO. COMO PODERIA RESOLVER ISSO?
Então já consegui efetuar esse select, com 3 tabelas. Segue o código abaixo.
UPDATE PEC_ITEM_ESTOQUE A
SET A.BASE_PIS = '0,00'
WHERE A.POSICAO_FISCAL = 40169990
AND EXISTS (
SELECT 1
FROM FAT_MOVIMENTO_ITEM B
INNER JOIN FAT_MOVIMENTO_CAPA C ON C.NUMERO_NOTA_FISCAL = B.NUMERO_NOTA_FISCAL
WHERE A.ITEM_ESTOQUE = B.ITEM_ESTOQUE
AND C.DTA_ENTRADA_SAIDA BETWEEN TO_DATE('12/08/2013', 'DD/MM/YYYY') AND TO_DATE('12/08/2013', 'DD/MM/YYYY')
AND C.REVENDA = 1
AND C.TIPO_TRANSACAO = 'P01'
)
Agora estou com dificuldades para duas tabelas.
EXEMPLO:
FAT_MOVIMENTO_ITEM E FAT_MOVIMENTO_CAPA O ÚNICO CAMPO EM COMUM NAS DUAS TABELAS É O NUMERO_NOTA_FISCAL
SELECT *
FROM FAT_MOVIMENTO_ITEM A
WHERE A.ITEM_ESTOQUE = 31025
AND EXISTS ( select A.Item_Estoque from fat_movimento_capa C
where C.dta_entrada_saida between to_date('12/08/2013','dd/mm/yyyy')
and to_date('12/08/2013','dd/mm/yyyy')
and C.Status = 'F'
and C.revenda = 1
and A.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL
and C.Tipo_Transacao = 'P01'
)
PORÉM O ÚNICO CRITÉRIO QUE ELE ESTÁ ACEITANDO É ESSE "and A.Numero_Nota_Fiscal = C.NUMERO_NOTA_FISCAL" , OUTROS COMO "and C.Status = 'F'" , 'and C.Tipo_Transacao = 'P01'" , "and C.revenda = 1" ELE ESTÁ IGNORANDO. COMO PODERIA RESOLVER ISSO?
Responder
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)