SELECT q.Branch
, q.[Teller Number]
, q.[Account Number]
, COUNT(q.Shares) OVER (PARTITION BY q.[Account Number]) AS [Shares]
, q.SharesBalance
, q.[Share Type]
, q.[Share Description]
FROM(
SELECT DISTINCT
s.branch AS [Branch]
, s.CREATEDBYUSER AS [Teller Number]
, acct.accountnumber AS [Account Number]
, COUNT(DISTINCT CASE WHEN s.CLOSEDATE is null
AND s.CHARGEOFFDATE is NULL
AND (s.OPENDATE >= @StartDate AND s.OPENDATE <= @EndDate)
then s.PARENTACCOUNT
END) AS [Shares]
, CASE WHEN s.CLOSEDATE is null
AND s.CHARGEOFFDATE is NULL
THEN s.balance
else 0
END AS [SharesBalance]
, CASE WHEN s.CLOSEDATE is null
AND s.CHARGEOFFDATE is NULL
THEN s.TYPE
ELSE 0
END AS [Share Type]
, CASE WHEN s.CLOSEDATE is null
AND s.CHARGEOFFDATE is NULL
then s.DESCRIPTION
ELSE NULL
END AS [Share Description]
from ARCUSYM000.dbo.ACCOUNT acct
left join ARCUSYM000.dbo.savings s
on acct.ACCOUNTNUMBER = s.PARENTACCOUNT and s.ProcessDate = acct.ProcessDate
where acct.CLOSEDATE is null
AND s.CLOSEDATE is null
AND s.chargeoffdate is null
AND ((s.OPENDATE >= @StartDate AND s.OPENDATE <= @EndDate)
OR (acct.OPENDATE >= @StartDate AND acct.OPENDATE <= @EndDate))
AND acct.memberstatus = 0
AND s.branch is not null
AND s.branch IN (@BranchNum)
AND s.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 112)
AND acct.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 112)
GROUP BY s.BRANCH
, s.CREATEDBYUSER
, acct.ACCOUNTNUMBER
, s.CLOSEDATE
, s.CHARGEOFFDATE
, s.BALANCE
, s.TYPE
, s.DESCRIPTION
, acct.STATEMENTMAILCODE
, s.SHARECODE
, s.OVERDRAFTTOLERANCE
, s.OPENDATE
, s.PARENTACCOUNT
) q
GROUP BY q.Branch
, q.[Teller Number]
, q.[Account Number]
, q.SharesBalance
, q.Shares
, q.[Share Type]
, q.[Share Description]
Я сделал подзапрос из своего запроса и получил желаемый результат.