I have multiple tables with data on some clients. What I want to achieve is to output the amount that a certain client was billed for, in the first month he was billed.
So I run the code similar to the one bellow:
SELECT company,clientid,COALESCE (signed.value,reactivated.value) as 'Activation' , Amount FROM `tblclients`
LEFT JOIN tblcustomfieldsvalues as signed ON tblclients.clientid = signed.relid and signed.fieldid = 5
LEFT JOIN tblcustomfieldsvalues as reactivated ON tblclients.userid = reactivated.relid and reactivated.fieldid = 27
LEFT JOIN (
SELECT clientid,sum(total) as Amount FROM tblinvoices
WHERE month(invoicedate)=month(Activation) Group by clientid) as f on tblclients.clientid = f.clientid
My problem is that when I do the last join it gives an error : Unknown column 'Activation' in 'where clause'. If I switch that to current_date the rest of the query works.
Any idea on how to make this work ?
Later edit: I might have oversimplified the query, I also have a COALESCE