Page 1 of 1

Joining on subqueries

Posted: 11 Nov 2012, 10:35
by taxrebel
I need to add the date of the last communication with a customer to an aged debtors report. Of course if I use a regular join to the contact_history table for this I end up duplicating the financial records by the number of communication events.
Using regular SQL this could be done with a subquery; something like:
INNER JOIN (SELECT refn, max(contact_date) FROM contact_history GROUP BY refn) last ON sales_ledger.account_ref = last.refn
Is it possible to achieve something similar in Report Designer?

Re: Joining on subqueries

Posted: 14 Nov 2012, 18:46
by brucedenney
Yes and No it is possible but...

You have to accept that it will return a huge amount of data, if you make sure the data is sorted by contact date, then create a group for the contact, hide all the details and only show the group footer, in the group footer when you refer to the contact, it will be the last contact it saw, which in turn id the last dated contact.

Nasty and inefficient but it will work.

Re: Joining on subqueries

Posted: 15 Nov 2012, 08:18
by taxrebel
Yes, it does. As you say it is horribly inefficient though. It's a shame there isn't more user control over the underlying SQL.

Re: Joining on subqueries

Posted: 15 Nov 2012, 11:09
by brucedenney
The fundamental problem is that the database is not an sql database it is a series of flat files with indexes.