0
I need to make one SELECT
using 2 tables with LEFT JOIN
, I successfully get the query, but it brings me some columns with wrong results.
The results of the First table are:
tbEntregas
--Manupart----sum(Qtd)---avg(Custounit)-----sum(Custototal)-----Count(Id)
---RC 018----------258--------------18.768--------------4939.70---------------43-----
The results of the second table are:
tbCustosProd
--Manupart----sum(Qtd)----avg(Valorunit)-----sum(Total value)------Count(Id)
---RC 018----------164--------------10.214---------------1694.81---------------9------
The Query with LEFT JOIN
brings the results where I make the use of SUM()
in a strange way; it makes the Sum of all fields where the ManuPart
are equal (so far all right), but it multiplies the result of the sum with the amount of records that exist of the same ManuPart
from the other table.
Something like this:
Select sum(Qtd) *
(select count(Id) from tbEntregas where ManuPart = 'RC 018')
from tbCustosProd
where ManuPart = 'RC 018'
My complete Query is:
select entregas.ManuPart,
sum(entregas.Qtd),
avg(entregas.ValorUnit),
sum(entregas.ValorTotal),
sum(custos.Qtd),
avg(custos.CustoUnit),
sum(custos.CustoTotal)
from tbEntregas entregas
left join tbCustosProd custos
on entregas.ManuPart = custos.ManuPart
and entregas.Qtd > 0
group by entregas.ManuPart;
These are the results I got:
deliveries.Manupart = RC 018 right.
Sum(deliverables.Qty) = 2322 wrong.
avg(deliveries.Valorunit) = 18.768 right.
sum(total value) = 44457.30 wrong.
sum(costs.Qty) = 7052 wrong.
avg(costs.Custounit) = 10.214 right.
sum(costs.Custototal) = 72876.83 wrong.
Mauricio, can you make the create table of your tables available together with an Internet data? Just to recreate your example and try to replicate the "error", the way it is in the question, even editing it, is still a little confused.
– MarceloBoni
Create the structure in http:/sqlfiddle.com/
– Tiedt Tech
@Marceloboni, CREATE TABLE
tbEntregas
(Id
int(11) NOT NULL AUTO_INCREMENT,ManuPart
varchar(255) DEFAULT NULL,Qtd
int(11) DEFAULT NULL,ValorUnit
double(9,2) DEFAULT NULL,ValorTotal
double(9,2) DEFAULT NULL, PRIMARY KEY (Id
) ) ENGINE=Myisam AUTO_INCREMENT=9997 DEFAULT CHARSET=latin1;– Maurício Sanches
@Marceloboni, CREATE TABLE
tbCustosProd
(Id
int(1) NOT NULL AUTO_INCREMENT,ManuPart
varchar(255) DEFAULT NULL,Qtd
int(11) DEFAULT NULL,CustoUnit
double(9,2) DEFAULT NULL,CustoTotal
double(9,2) DEFAULT NULL, PRIMARY KEY (Id
) ) ENGINE=Myisam AUTO_INCREMENT=2758 DEFAULT CHARSET=latin1;– Maurício Sanches
first I think the structure of your tables are confusing, for example.. why do you have a total value column? If you have the quantity and unit value, this column can be calculated and not inserted in a column.
– aa_sp
(continuing) Second, what exactly is your tbCustosProd table? I understood that Voce has a table to record deliveries and another reference to know the unitary value of each product (tbCustosProd) so why do you have Custototal and quantity in tbCustosProd? Third, why don’t you use a number instead of text as a key? I created a Fiddle to make it easier for us http://sqlfiddle.com/#! 18/c59ee/3
– aa_sp