The SQL is ridiculously long and messy....
it is as follows:
SELECT Rid.Dur1, Rid.Code, Sum(1*IIf([Rid].[layer]=0 And [Rid].[code]=[Rid].[rcode] And ([rid].[status]="I" Or ([rid].[status]="P" And (Not IsEmpty([Rid].[edate])))),1,0)) AS nopol, Sum([rid].[bsa]*1*IIf([Rid].[code]=[Rid].[rcode]...