我试图从两张工作桌上抽取,我挣扎!
我用笔记本、一张与接触链接的表格、一张与客户联系的表格和一个连接客户地址的表格。 我试图做些什么是,发现每个员额代码有多少次由联系人和客户分类的邮件。
Postcode | Count of Clients | Count of Contacts
UB | 123 | 12345
HA | 223 | 22345
但是,显然只有接触者和客户得到同样的重视。 当我总是错过错时,在试图将它列入一些空洞的声明时,我感到可怕的困惑。
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