INNER JOIN with SOMA - SQL

Asked

Viewed 32 times

0

Good morning!

I have two tables in access that present me CPF and value, but I am not able to join the two tables adding the amount. As I do not understand access, I am trying to make the query via SQL (which by the way I also do not have so much knowledge):

Ex.:

CPF (tabela1)                         CPF (tabela2)
----------------------                ----------------------
12345678910     10,00                 12345678910     9,00
01987654321     20,00                 01987654321     20,00

The query would actually subtract one field from the other and return anything other than zero. In the table example, he would have to show me the first line with the value of 1.00 and not bring the second line.

SELECT CPF, vlrTRAN 
FROM Arqtxt 
INNER JOIN Arqtxt2 ON Arqtxt.[CPF] = Arqtxt2.NumCpf;
  • Fixing code: SELECT CPF, vlrTRAN FROM Arqtxt INNER JOIN Arqtxt2 ON Arqtxt. [CPF] = Arqtxt2.Numcpf;

  • you can edit the question (I already put this code in place of the other)

1 answer

1


The solution presented is for the two tables below, same problem presented by changing the name of the tables and the columns.

**tabela1**
cpf         valor
12345678910   10
01987654321   20


**tabela2**
cpf          valor
12345678910  9
01987654321  20

Query sql:

SELECT t1.cpf, t1.valor, (t1.valor - t2.valor) as diferenca 
FROM tabela1  as t1
INNER JOIN tabela2 as t2 ON t1.cpf = t2.cpf
where (t1.valor - t2.valor) > 0

First of all put alias for the tables, this avoids ambiguity in the query because the tables have the columns with the same name. Subtract the column value of Tabela1(t1) and table2(t2). At the end put a where to filter only values greater than 0

  • It was exactly what I needed! Thank you very much Bernardo, saved my day!!!! I will study further to reach a conclusion as simply as you arrived!!! Thanks

  • w3school is good to study sql

  • Edit: another question... can I also show what you don’t have in both tables? Ex. have three CPF’s in one and two in the other?

  • @Douglassilva is possible yes. Does a search in google "except sql", which finds a lot

Browser other questions tagged

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