select A.ClientID, A.ClientSince,
sum(case when B.ProductCode = 'Product1' then 1 else 0 end) Product1,
sum(case when B.ProductCode = 'Product2' then 1 else 0 end) Prodcut2
from clients A
left join accounts B on A.ClientID = B.ClientID
WHERE (A.ClientSince <= CONVERT(DATETIME, '2010-07-01 00:00:00', 102))
AND (B.CommenceDate <= CONVERT(DATETIME, '2010-07-01 00:00:00', 102))
AND B.AccountBal > 0
group by A.ClientID, A.ClientSince
order by A.ClientID
|