I'm just going to post what I have which is a script that doesn't work as I expect.SELECT ET.ey_id, EO.eo_id, OP.op_id, DI.di_id, (OP.op_FirstName + ' ' + OP.op_LastName + '+' + DI.di_FirstName + ' ' + DI.di_LastName) AS TestTeam, (SELECT SUM(pa_VoucherCode.vc_Value) AS DispensingVoucher FROM pa_DispensingClaim AS DC INNER JOIN pa_VoucherCode ON DC.dc_vcid = pa_VoucherCode.vc_id WHERE (DC.disabled = 0) AND (DC.dc_eoid = EO.eo_id)) AS DispenseVoucherAmount, (SELECT SUM(pa_TintClaimCodes.tc_Value) AS DispensingTint FROM pa_DispensingClaim AS DC INNER JOIN pa_TintClaimCodes ON DC.dc_tcid = pa_TintClaimCodes.tc_id WHERE (DC.disabled = 0) AND (DC.dc_eoid = EO.eo_id)) AS TintAmount, (SELECT SUM(pa_PrismClaimCodes.pl_Value) AS DispensingPrism FROM pa_DispensingClaim AS DC INNER JOIN pa_PrismClaimCodes ON DC.dc_plid = pa_PrismClaimCodes.pl_id WHERE (DC.disabled = 0) AND (DC.dc_eoid = EO.eo_id)) AS PrismAmount, (SELECT SUM(cc_value) AS STClaim FROM pa_STClaimCode CC WHERE (cc.cc_id = SC.sc_ccid)) AS STClaimAmountFROM pa_EyeTest AS ET INNER JOIN pa_EyeOrder AS EO ON ET.ey_id = EO.eo_eyid INNER JOIN pa_Optician AS OP ON ET.ey_optician = OP.op_id INNER JOIN pa_Dispenser AS DI ON ET.ey_dispenser = DI.di_id INNER JOIN pa_STClaim as SC ON ET.ey_id = SC.sc_eyidWHERE (EO.disabled = 0)
The Query should output one record per entry in PA_EYETEST(ET), PA_EYEORDER(EO) will have multiple records for each parent record, and I am subquerying linked tables to this for aggregates. PA_STCLAIM also has multiple records per parent record.I have more joins to add to this but I am struggling to get something like the following:EY_ID EO_ID SUM(DispenseVoucherAmount) .... SUM(STClaimAmount)There are multiple records as there may be two child ORDERS and three STCLAIMS, I think the problem is that I am trying to join queries of different shapes but I'm hoping someone here can make sense of where I'm going wrong.
I have been working on this for 5 days trying a whole manner of different approaches and am currently in despair...Any help, even to say I can't do this would be welcome.