我使用以下SQL查询:
select
FilteredSystemUser.systemuserid,
FilteredPhoneCall.regardingobjectid
from
FilteredPhoneCall
Inner Join
FilteredSystemUser on FilteredSystemUser.systemuserid in (FilteredPhoneCall.createdby)
Inner Join
FilteredLead on FilteredLead.leadid = FilteredPhoneCall.regardingobjectid
where
DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = 0
and FilteredLead.statecode = 0
and FilteredLead.ownerid = FilteredSystemUser.systemuserid
and FilteredPhoneCall.statecode = '1'
and FilteredPhoneCall.regardingobjecttypecode = 4
and FilteredPhoneCall.createdby in ('c2dd1ddc-0374-e611-80dc-00155d3d1992', '53cfbe3a-a09a-e611-80df-00155dce24d0')
并获得以下输出
在这里,我对于相同系统用户的 97C29D24-BEE2-E611-80F1-00155DCE24EF,091B1CAB-C2E2-E611-80F1-00155DCE24EF 有两个条目。所以我需要将其视为单个条目并获得除此之外的计数,两个系统用户有另外两个ID。
答案 0 :(得分:1)
要在此处计算列引用的不同值的计数... SQL: Count distinct values from one column based on multiple criteria in other columns
对于您的情况,请尝试:
select
FilteredSystemUser.systemuserid,
Count(DISTINCT FilteredPhoneCall.regardingobjectid ) as NoofCall
from
FilteredPhoneCall
Inner Join
FilteredSystemUser on FilteredSystemUser.systemuserid in (FilteredPhoneCall.createdby)
Inner Join
FilteredLead on FilteredLead.leadid = FilteredPhoneCall.regardingobjectid
where
DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = 0
and FilteredLead.statecode = 0
and FilteredLead.ownerid = FilteredSystemUser.systemuserid
and FilteredPhoneCall.statecode = '1'
and FilteredPhoneCall.regardingobjecttypecode = 4
and FilteredPhoneCall.createdby in ('c2dd1ddc-0374-e611-80dc-00155d3d1992', '53cfbe3a-a09a-e611-80df-00155dce24d0')
Group by FilteredSystemUser.systemuserid
答案 1 :(得分:0)
您可以使用count()和group by来实现此目的。
select COUNT(DISTINCT FilteredPhoneCall.regardingobjectid ) as NoofCall
,FilteredSystemUser.systemuserid
from FilteredPhoneCall
inner Join FilteredSystemUser
on FilteredSystemUser.systemuserid in(FilteredPhoneCall.createdby)
Join FilteredLead
on FilteredLead.leadid = FilteredPhoneCall.regardingobjectid
where DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = 0
and FilteredLead.statecode = 0 and FilteredLead.ownerid = FilteredSystemUser.systemuserid
and FilteredPhoneCall.statecode = '1' and FilteredPhoneCall.regardingobjecttypecode = 4
and FilteredPhoneCall.createdby in ('c2dd1ddc-0374-e611-80dc-00155d3d1992','53cfbe3a-a09a-e611-80df-00155dce24d0')
and FilteredSystemUser.systemuserid in ('c2dd1ddc-0374-e611-80dc-00155d3d1992','53cfbe3a-a09a-e611-80df-00155dce24d0')
GROUP BY FilteredSystemUser.systemuserid
答案 2 :(得分:0)
尝试这个
;with CTE
AS (
select
FilteredSystemUser.systemuserid,
FilteredPhoneCall.regardingobjectid
from
FilteredPhoneCall
Inner Join
FilteredSystemUser on FilteredSystemUser.systemuserid in (FilteredPhoneCall.createdby)
Inner Join
FilteredLead on FilteredLead.leadid = FilteredPhoneCall.regardingobjectid
where
DateDiff(d, FilteredPhoneCall.createdon, GETDATE()) = 0
and FilteredLead.statecode = 0
and FilteredLead.ownerid = FilteredSystemUser.systemuserid
and FilteredPhoneCall.statecode = '1'
and FilteredPhoneCall.regardingobjecttypecode = 4
and FilteredPhoneCall.createdby in ('c2dd1ddc-0374-e611-80dc-00155d3d1992', '53cfbe3a-a09a-e611-80df-00155dce24d0') )
select count(CTE.systemuserid) as myCOUNT,CTE.systemuserid,CTE.regardingobjectid from CTE
group by CTE.systemuserid,CTE.regardingobjectid