4,707 questions
Hi @mehmood tekfirst ,
As JingyangLi said, you can try the following code:
DECLARE
@s nvarchar(MAX) = N'',
@firstName nvarchar(40) = N'omer';
SET @s = N' ;WITH CTECount AS (
Select ba.Id, ar.RenterUserId,ad.MainDriverUserId,ba.RentalAgreementId, ba.AgreementStatus, ba.FranchiseId,
ROW_NUMBER() over (partition by ar.RenterUserId,ad.MainDriverUserId order by ar.RenterUserId desc) RowNo ,
(case when ba.RentalAgreementId <> " " then Count(ba.Id)
OVER (Partition by (case when ba.RentalAgreementId <> " " then ar.RenterUserId else 0 end)) else 0 end) RACount,
(case when ba.RentalAgreementId = " " then Count(ba.Id)
OVER (Partition by (case when ba.RentalAgreementId = " " then ad.MainDriverUserId else 0 end)) else 0 end) ResCount
from BookingAgreements ba with (nolock)
left join AgreementRenters ar with (nolock) on ar.AgreementId = ba.Id and ar.FranchiseId = ba.FranchiseId
left join AgreementDrivers ad with (nolock) on ad.AgreementId = ba.Id and ad.FranchiseId = ba.FranchiseId
) Select * FROM CTECount;'
EXEC sys.sp_executesql
@s;
Best regards
Niko