The style of join you are using is very old-fashioned, can become difficult to read and restricts you to only being able to use INNER joins. So instead of
select sum (Quantidade * Preco_unit * Iva /100 + (quantidade * preco_unit)) as 'TotalCIVA1'
from Torcamento_aux, Torcamentos_new
where TOrcamento_aux.Cod_Orc=Torcamentos_new.cod_orcamento;
do this instead
select sum (Quantidade * Preco_unit * Iva /100 + (quantidade * preco_unit)) as 'TotalCIVA1'
from Torcamento_aux
inner join Torcamentos_new on TOrcamento_aux.Cod_Orc=Torcamentos_new.cod_orcamento;
Use an ON clause to define the JOIN and a WHERE clause to define any actual filters.
If I was being honest I would probably also use table aliases in case the query is going to become more complex and explicitly indicate which table the columns I am using come from (to make the query more readable to others)
select sum (aux.Quantidade * aux.Preco_unit * aux.Iva /100 + (aux.quantidade * aux.preco_unit)) as 'TotalCIVA1'
from Torcamento_aux aux
inner join Torcamentos_new tnew on aux.Cod_Orc=tnew.cod_orcamento;
If you are getting all of the records from
TOrcamento_aux
then there must be a row on
Torcamentos_new
for every row in
TOrcamento_aux
- that is how an inner join works. Perhaps you need to check some other value. Some sample data might help us to help you further
Edit after OP comments to another solution:
joao pedro Jun2022 commented:
The table Torcamentos_new Has two records
Cod_orcamento = 16 and 47
The table Torcamento_aux Also has two records
Cod_orc = 16 and 47
Quantidade = 1 and 1
Preco_unit = 1 and 1
Iva = 23 and 23
As I suggested - the table Torcamentos_new has rows that match to every row in Torcamento_aux - therefore you will get every record from Torcamento_aux returned in your results. This article might help you understand why -
Visual Representation of SQL Joins[
^]
joao pedro Jun2022 commented:
Some records have been deleted for me to test
I suggest those records are put back into Torcamento_aux so you can see what is actually happening.
joao pedro Jun2022 commented:
Table Torcamento_aux is filtered by Torcamento_aux.cod_orc and Torcamentos_new.cod_orcamento using an Edit.text with the Torcamentos_new.cod_orcamento record, the filter is on afterscrool event of the table Torcamentos_new.
There is no such thing as Edit.text nor an afterscroll event in SQL. This makes no sense. The "filtering" takes place as part of the JOIN or as the result of a WHERE clause - not because a user has scrolled down in some secret software.
joao pedro Jun2022 commented:
I have tried the inner join code and the result os the same 'totalciva'=2,46 what do i need to do please?
The result will be the same because both tables have the same keys in them. Try adding a row to the Torcamento_aux table that does
not have a corresponding row in Torcamentos_new and you should be able to see the difference. There is nothing to do, the answer is correct - what were you expecting to see?
Edit 2 after further comment:
The term "filter" usually means to reduce the number of items based on some criteria.
In this case you mean GROUP BY see
GROUP BY (Transact-SQL) - SQL Server | Microsoft Docs[
^]
Try
select aux.Cod_orc,sum (aux.Quantidade * aux.Preco_unit * aux.Iva /100 + (aux.quantidade * aux.preco_unit)) as 'TotalCIVA1'
from Torcamento_aux aux
inner join Torcamentos_new tnew on aux.Cod_Orc=tnew.cod_orcamento
GROUP BY aux.Cod_Orc;