Я бы подошел к этому с помощью оператора выборочного подсчета. Как то так:
Declare @Temp
Table (
Customer VarChar(100),
SendingOrganization varchar(100),
Sender_Email_Address varchar(100)
);
Insert Into @Temp Values('entweiler','a1','processor x');
Insert Into @Temp Values('entweiler','a2','processor x');
Insert Into @Temp Values('entweiler','a3','processor x');
Insert Into @Temp Values('entweiler','a4','x@y.com');
Insert Into @Temp Values('entweiler','a4','processor x');
Insert Into @Temp Values('entweiler','a4','processor x');
Insert Into @Temp Values('entweiler','a4','processor x');
Insert Into @Temp Values('entweiler','a4','processor x');
Insert Into @Temp Values('entweiler','a5','a@b.com');
Insert Into @Temp Values('entweiler','a5','b@c.com');
Insert Into @Temp Values('entweiler','a5','c@d.com');
Insert Into @Temp Values('entweiler','a5','d@e.com');
Insert Into @Temp Values('entweiler','a5','e@f.comx');
Insert Into @Temp Values('entweiler','a5','processor x');
Insert Into @Temp Values('entweiler','a6','f@g.com');
With Emails As
(
Select SendingOrganization,
Count(Case when sender_email_address = 'processor x' Then 1 End) As WithProcessor,
Count(Case when sender_email_address <> 'processor x' Then 1 End) As WithoutProcessor,
Count(*) As AllEmail
From @Temp
Group By SendingOrganization
)
Select T.*,
Case When WithProcessor = AllEmail Then 1
When WithoutProcessor = allEmail Then 3
Else 2
End As Rank
From Emails
Inner Join @Temp T
On Emails.SendingOrganization = T.SendingOrganization