Fórum Somar campo usando Union #223808
01/04/2004
0
select CODIGO, NOME, QUANTIDADE from Vendas
UNION
select CODIGO, NOME, QUANTIDADE from Compras
CONTEUDO DAS TABELAS:
VENDAS
01 TELEFONE 1 (CODIGO, NOME, QUANTIDADE)
COMPRAS
01 TELEFONE 1 (CODIGO, NOME, QUANTIDADE)
Ou seja, tenho registros iguais para as duas tabelas. Eu preciso que o resultado da pesquisa some a quantidade em produtos iguais. Ex.
01 TELEFONE 2
Se eu utilizar apenas UNION ele mostra só um registro e se utilizar UNION ALL ele mostra os dois registros.
Alguem tem alguma dica???
Obrigado,
Mcd
Curtir tópico
+ 0Post mais votado
01/04/2004
select CODIGO, NOME, SUM(QUANTIDADE)
from (
select CODIGO, NOME, QUANTIDADE from Vendas
UNION
select CODIGO, NOME, QUANTIDADE from Compras
)
group by CODIGO, NOME
se for access, crie uma consulta com o select interno...
se for paradox... se mate.... hehehehe :lol:
Kotho
Gostei + 1
Mais Posts
01/04/2004
Mcd
Gostei + 0
01/04/2004
Mcd
Alguma idéia do que pode ter dado errado ?
Obrigado...
Gostei + 0
05/04/2004
Kotho
crie uma VIEW como comando:
CREATE VIEW NOME_DA_VIEW AS SELECT...
da faça:
SELECT ... FROM NOME_DA_VIEW ...
Gostei + 1
11/06/2016
Matheus Neves
estou seguido se raciocino se somar usando union, mas esta dando erro:
ERROR: syntax error at or near "group"
LINE 6: group by mes
^
********** Error **********
ERROR: syntax error at or near "group"
SQL state: 42601
Character: 293
olha com esta meu codigo:
select extract(month from data_emissao)as mes, sum(1*valor)
from(select extract(month from data_emissao), valor
from financeiro_entradas where data_baixa is not null) f union all
(select extract(month from data_emissao), valor*(-1)
from financeiro_saidas where data_baixa is not null)
group by mes
order by mes
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)