I'm trying to get a count from 2 tables working and I'm struggling!
I've got a table with notebook items, a table with links to contacts, a table linking contacts to clients and a table linking clients to addresses. What I'm trying to do is find out how many times each postcode has been mailshotted grouped by contact and client.
Postcode | Count of Clients | Count of Contacts
UB | 123 | 12345
HA | 223 | 22345
But am, obviously, only getting the same count for both contacts and clients. I'm getting horrifically confused when trying to put it into nested select statements as I'm always getting errors, so would appreciate any help going!
select COUNT (ni.NotebookItemId) AS Clients,
COUNT(nl.ObjectId) AS Contacts,
Substring(ad.PostCode, 1, 2) AS Postcode
from NotebookItems ni
inner join notebooklinks nl on ni.NotebookItemId = nl.NotebookItemId
inner join ClientContacts cc on cc.ContactPersonId = nl.ObjectId
inner join address ad on ad.ObjectId = cc.ClientId
where NotebookTypeId = 75 AND ni.CreatedOn > 'mar 16 2012'
AND (ad.postcode like 'UB%' or
ad.postcode like 'HA%')
GROUP BY Substring(ad.PostCode, 1, 2)
order by Contacts desc