English 中文(简体)
在一份说明中计算2个不同栏目
原标题:Getting the count of 2 different columns in one statement

我试图从两张工作桌上抽取,我挣扎!

我用笔记本、一张与接触链接的表格、一张与客户联系的表格和一个连接客户地址的表格。 我试图做些什么是,发现每个员额代码有多少次由联系人和客户分类的邮件。

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
最佳回答

Count() will count all non null rows, as Andrew says you might be after: COUNT(DISTINCT thing) i.e.:

select COUNT (DISTINCT ni.NotebookItemId) AS Clients, 
COUNT(DISTINCT nl.ObjectId) AS Contacts, 
Substring(ad.PostCode, 1, 2) AS Postcode
...

然后,你将得出每个领域两个不同的价值观。

I m not sure this is what you want. You say:

What I m trying to do is find out how many times each postcode has been mailshotted grouped by contact and client.

我期望问一下这方面的数据:

SELECT 
      Count(MailshotID) AS Count
    , Substring(address.PostCode, 1, 2) AS Postcode
    , ContactID
    , ClientID
FROM
    -- your joined tables here
WHERE
    -- your criteria here
GROUP BY 
      Substring(address.PostCode, 1, 2)
    , ContactID
    , ClientID

获得:

Count   Postcode   ContactID  ClientID
3       UB         201        301
6       HA         202        302
2       UB         203        305
18      UB         203        306

每个员额代码、联系人、客户或按邮政编码、联系人和客户分类的邮件数。

问题回答

暂无回答




相关问题
SELECT command to calculate percentage

I m trying to get the percentage of each video I have in my database based on its view count against all other videos. I m then trying to display all the videos from highest view count to lowest, ...

Consolidating a COUNT query

I have a page where I am running an initial SQL query to get a list of subjects, then I loop over this query and run two additional queries for each record returned from the original subjects query (I ...

R: Count number of objects in list [closed]

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) ...

Mysql get count of rows for each day

My Current query is: SELECT DISTINCT DATE(vote_timestamp) AS Date, COUNT(*) AS TotalVotes FROM `votes` WHERE vote_target_id= 83031 GROUP BY DATE(vote_timestamp) ORDER BY DATE(vote_timestamp) DESC ...

Group by named column

I always forget how to do things like this. I have a database table with birthdates and I want to find out how many people have the same age. I m trying: SELECT TIMESTAMPDIFF( YEAR, birthdate, ...

TableView oval button for Index/counts

Can someone help me create an index/count button for a UITableView, like this one? iTunes http://img.skitch.com/20091107-nwyci84114dxg76wshqwgtauwn.preview.jpg Is there an Apple example, or other ...

热门标签