Building: Im trying to build a report where I can calculate the difference between two dates (Example between 1-1-2001 and 31-12-2001) but also showing (per name per identifier) the current balance of a customer per date(As shown in the Report attachment).
Problem: The problem is that the totals are not correct, they get multiplied on random numbers (Checking it with Count and CountDistinct I learned that I probably made a mistake in my SQL, so I copied SQL in this question as well).
I have tried plenty myself and tried to find solutions on the interwebs, but none of them seem to work. So I am desperate enough to ask for help :).
SELECT CASE WHEN t1.identifier IS NULL THEN t2.identifier ELSE t1.identifier END as identifier , CASE WHEN t1.firstName IS NULL THEN t2.firstName ELSE t1.firstName END as firstName , CASE WHEN t1.lastName IS NULL THEN t2.lastName ELSE t1.lastName END as lastName , CASE WHEN t1.initials IS NULL THEN t2.initials ELSE t1.initials END as initials , CASE WHEN t1.name IS NULL THEN t2.name ELSE t1.name END as name , CASE WHEN t1.employmentNumber IS NULL THEN t2.employmentNumber ELSE t1.employmentNumber END as employmentNumber , CASE WHEN t1.externalIdentifier IS NULL THEN t2.externalIdentifier ELSE t1.externalIdentifier END as externalIdentifier , t1.balance AS S_balance , t1.unvested AS S_unvested , t2.balance AS V_unvested , t2.unvested AS V_balance , SUM(t1.TotalBalance + t1.TotalUnvested) Total1 FROM ( SELECT par.identifier ,par.firstName ,par.lastName ,par.initials ,pos.name ,pos.balance ,pos.unvested ,par.employmentNumber ,SUM(pos.balance) TotalBalance ,sum(pos.unvested) TotalUnvested ,Right(par.externalIdentifier,(Len(par.externalIdentifier)-4)) as externalIdentifier FROM participant par INNER JOIN position pos ON par.identifier = pos.participantIdentifier WHERE (pos.balance != 0 OR pos.unvested != 0) AND pos.name NOT LIKE (@Euro) AND par.Metadata_start_validity <=@Datum AND (par.Metadata_end_validity >=@Datum OR par.Metadata_end_validity IS NULL) AND pos.Metadata_start_validity <=@Datum AND (pos.Metadata_end_validity >=@Datum OR pos.Metadata_end_validity IS NULL) GROUP BY par.identifier ,par.firstName ,par.lastName ,par.initials ,pos.name ,pos.balance ,pos.unvested ,par.employmentNumber ,par.externalIdentifier ) as t1 INNER JOIN ( SELECT par.identifier ,par.firstName ,par.lastName ,par.initials ,pos.name ,pos.balance ,pos.unvested ,par.employmentNumber ,Right(par.externalIdentifier,(Len(par.externalIdentifier)-4)) as externalIdentifier FROM participant par INNER JOIN position pos ON par.identifier = pos.participantIdentifier WHERE (pos.balance != 0 OR pos.unvested != 0) AND pos.name NOT LIKE (@Euro) AND par.Metadata_start_validity <=@V_Datum AND (par.Metadata_end_validity >=@V_Datum OR par.Metadata_end_validity IS NULL) AND pos.Metadata_start_validity <=@V_Datum AND (pos.Metadata_end_validity >=@V_Datum OR pos.Metadata_end_validity IS NULL) ) as t2 ON t1.identifier = t2.identifier GROUP BY CASE WHEN t1.identifier IS NULL THEN t2.identifier ELSE t1.identifier END , CASE WHEN t1.firstName IS NULL THEN t2.firstName ELSE t1.firstName END , CASE WHEN t1.lastName IS NULL THEN t2.lastName ELSE t1.lastName END , CASE WHEN t1.initials IS NULL THEN t2.initials ELSE t1.initials END , CASE WHEN t1.name IS NULL THEN t2.name ELSE t1.name END , CASE WHEN t1.employmentNumber IS NULL THEN t2.employmentNumber ELSE t1.employmentNumber END , CASE WHEN t1.externalIdentifier IS NULL THEN t2.externalIdentifier ELSE t1.externalIdentifier END , t1.balance , t1.unvested , t2.balance , t2.unvested , t1.TotalBalance , t1.TotalUnvested