I'm using Crystal SQL Designer to create my SQL. This seems to be working, but on some records the result of the SUM function is being doubled - but not all records. I've checked the data and it all looks fine, so I don't quite understand what's going on. Here's my SQL:
SELECT DISTINCT Funeral1.`Codnum`, Funeral1.`Funnum`, Funeral1.`Source`, Funeral1.`Invoiced`, Funeral1.`Dec_Sur`, Funeral1.`Dis_Typ`, Funeral1.`Fun_typ`, Funeral1.`Fun_dir`, Funeral1.`Ser_dat`, Funeral1.`Casketcode`, Sum(IIf(charges1.`category`<>"CREMAT",charges1.`amount`,0)) AS TOTALSRVC, Sum(IIf(charges1.`category`="CREMAT",charges1.`amount`,0)) AS CREMATFEE,
Sum(IIf(merchandise1.`type`=1 AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS CASKETCOST,
Sum(IIf(merchandise1.`itype`="U" AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS URNCOST,
Sum(IIf(merchandise1.`itype`="V" AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS OUTERCOST
FROM (`Funeral1` Funeral1 LEFT JOIN `charges1` charges1 ON Funeral1.`Codnum` = charges1.`number`) LEFT JOIN `merchandise1` merchandise1 ON charges1.`number` = merchandise1.`number`
GROUP BY Funeral1.`Codnum`, Funeral1.`Funnum`, Funeral1.`Invoiced`, Funeral1.`Dec_Sur`, Funeral1.`Dis_Typ`, Funeral1.`Fun_typ`, Funeral1.`Fun_dir`, Funeral1.`Ser_dat`
Thanks.
SELECT DISTINCT Funeral1.`Codnum`, Funeral1.`Funnum`, Funeral1.`Source`, Funeral1.`Invoiced`, Funeral1.`Dec_Sur`, Funeral1.`Dis_Typ`, Funeral1.`Fun_typ`, Funeral1.`Fun_dir`, Funeral1.`Ser_dat`, Funeral1.`Casketcode`, Sum(IIf(charges1.`category`<>"CREMAT",charges1.`amount`,0)) AS TOTALSRVC, Sum(IIf(charges1.`category`="CREMAT",charges1.`amount`,0)) AS CREMATFEE,
Sum(IIf(merchandise1.`type`=1 AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS CASKETCOST,
Sum(IIf(merchandise1.`itype`="U" AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS URNCOST,
Sum(IIf(merchandise1.`itype`="V" AND merchandise1.`number`=Funeral1.`Codnum`,merchandise1.`price`,0)) AS OUTERCOST
FROM (`Funeral1` Funeral1 LEFT JOIN `charges1` charges1 ON Funeral1.`Codnum` = charges1.`number`) LEFT JOIN `merchandise1` merchandise1 ON charges1.`number` = merchandise1.`number`
GROUP BY Funeral1.`Codnum`, Funeral1.`Funnum`, Funeral1.`Invoiced`, Funeral1.`Dec_Sur`, Funeral1.`Dis_Typ`, Funeral1.`Fun_typ`, Funeral1.`Fun_dir`, Funeral1.`Ser_dat`
Thanks.