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”。