我的查询有问题,通过join多个表使用sum或count数据,查询的结果应该不匹配,结果相乘,使用join多个表计算数据时应该考虑什么,就足够了让我困惑
像这样 :
我有 4 个相互连接的表
select T3.series,
sum(If(Month = 1 and Year = 2013,jumlah,0)) as 'JAN 13',
sum(If(Month = 2 and Year = 2013,jumlah,0)) as 'FEB 13',
sum(If(Month = 3 and Year = 2013,jumlah,0)) as 'MAR 13'
From Typemotor T3
LEFT JOIN (
select T.Series,Month(C.Date) as Month,Year(C.Date) as Year,
Count(T.Series) as Jumlah
from
(select a.Type, a.Date,a.MachineNumber
from
selling_cash a
left join lokasi b
on a.Showr = b.KodeLokasi
where b.PT = 'PT MERDEKA'
group by a.MachineNumber)
as C
Left Join Typeconvert T2
on C.Type = T2.TypeSystem
Left Join Typemotor T
on T2.TypeConv = T.Type
Group by T.Series,Month,Year ) as R
On R.Series = T3.Series
Group by T3.Series order by T3.Series asc;
结果是:
+--------------------+--------+--------+--------+
| series | JAN 13 | FEB 13 | MAR 13 |
+--------------------+--------+--------+--------+
| AUTOMATIC LPM | 13068 | 9416 | 10648 |
| AUTOMATIC PREMIUM | 187 | 506 | 330 |
| AUTOMATIC STANDART | 11502 | 12690 | 10206 |
| JUPITER SERIES | 3584 | 3360 | 3360 |
| LEXAM | 0 | 0 | 0 |
| MX SERIES | 3456 | 1664 | 3200 |
| SPORT | 9620 | 7852 | 7826 |
| VEGA SERIES | 2808 | 1728 | 1080 |
+--------------------+--------+--------+--------+
结果应该是
+--------------------+--------+--------+--------+
| series | JAN 13 | FEB 13 | MAR 13 |
+--------------------+--------+--------+--------+
| AUTOMATIC LPM | 33 | 28 | 38 |
| AUTOMATIC PREMIUM | 3 | 9 | 6 |
| AUTOMATIC STANDART | 35 | 41 | 32 |
| JUPITER SERIES | 22 | 20 | 20 |
| LEXAM | 0 | 0 | 0 |
| MX SERIES | 27 | 13 | 25 |
| SPORT | 80 | 64 | 64 |
| VEGA SERIES | 13 | 8 | 5 |
+--------------------+--------+--------+--------+
是否有任何建议,指出需要考虑什么来执行连接表的计算/求和以使结果正确?