DECLARE @StartDate AS SMALLDATETIME
DECLARE @EndDate AS SMALLDATETIME
DECLARE @BranchNum AS INT
SET @StartDate = '2019-10-22'
SET @EndDate = '2019-10-23'
set @BranchNum = '4'
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],
CASE When c.TYPE in (15,16,17)
AND c.STATUS = 1
AND c.CLOSEDATE is null
AND (c.ISSUEDATE >= @StartDate AND c.ISSUEDATE <= @EndDate)
THEN 1
ELSE 0
END AS [Debit]
from ARCUSYM000.dbo.ACCOUNT acct
INNER join ARCUSYM000.dbo.savings s
on acct.ACCOUNTNUMBER = s.PARENTACCOUNT
and s.ProcessDate = acct.ProcessDate
INNER join arcuSYM000.dbo.CARD c
on acct.ACCOUNTNUMBER = c.PARENTACCOUNT
and c.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 = @BranchNum
AND s.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()), 112)
AND acct.ProcessDate = CONVERT(VARCHAR(8), dateadd(day,-1, getdate()),
112)
AND c.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, c.TYPE,
c.STATUS, c.USAGEDATE, c.CLOSEDATE, c.ISSUEDATE, acct.STATEMENTMAILCODE,
s.SHARECODE, s.OVERDRAFTTOLERANCE, s.OPENDATE, s.PARENTACCOUNT
ORDER BY Branch, [Teller Number], [Account Number]
Прямо сейчас я получаю «1» для обеих строк в столбце «Дебет», если есть две строки с одинаковым номером счета. Могу ли я получить только одну «1», если есть несколько записей с одним номером счета? Я не уверен, нужно ли мне сделать весь этот запрос подзапросом и создать случай вне этого, чтобы получить только один «1» в результате.
Мой текущий результат:
Мой желаемый результат: