我想创建一个结果,列出昨天添加到数据库的所有客户端,然后是同一组客户端的单独列,但只有那些没有电子邮件地址的列,所以我们可以看到哪个CSR代表没有添加电子邮件地址,我在编写子查询时遇到问题。下面是我的代码,它使用两个表CLIENT和CONTACT,它们在ACCT_NO字段上加入
我正在使用SQL Server Management Studio
select
count(distinct c.acct_no) as Total, ADDED_BY
from
client c
inner join
(select
count(distinct ct.acct_no) acct_NO
from
contact CT
inner join
client on client.acct_no = ct.ACCT_NO
and ct.EMAILADDR1 = ''
and ct.[PRIMARY] = 'A'
group by
ct.ACCT_NO) x on c.ACCT_NO = x.acct_NO
where
c.DATE_ADDED = CONVERT(varchar(10), GETDATE()-10, 121)
group by
c.ADDED_BY
答案 0 :(得分:0)
您可以使用count(distinct case ...
作为条件总和,例如:
select ADDED_BY
, count(distinct case when client.EMAILADDR1 is null then client.ACCT_NO end)
as TotalWithoutMail
, count(distinct case when client.EMAILADDR1 is not null then client.ACCT_NO end)
as TotalWithMail
from contact
jon client
on client.ACCT_NO = contact.ACCT_NO
group by
ADDED_BY