Good day.
I have a query that I am having difficult time with:
Select
ah.MemberNumber,
ah.AccountNumber,
BIN = Case When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) In (543940,534025) Then 420477
When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) Is Null AND (Select Max(dc.CreditCardAccount) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.BIN = 581551 AND dc.TransactionDate > ah.EntryDate) = 0 Then 581551
When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) Is Null OR (Select Max(dc.CreditCardAccount) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.BIN = 420477) <> 0 Then 420477
Else (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber)
End,
Transaction_Summary = Case When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card Signature'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount < 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card PIN/PINLess'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount > 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - PIN/PINLess'
When ah.TransactionType = 'VCR' AND ah.FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - Debit Card Signature'
When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Debit'
When ah.TransactionType = 'VCR' AND ah. FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Credit'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount < 0 Then 'ATM Withdrawal'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount > 0 Then 'ATM Deposit'
Else 'None'
End,
Total_Count = SUM(Case When ah.PrincipalAmount <> 0 Then 1 Else 0 End),
Total_Amount = SUM(Case When ah.PrincipalAmount <> 0 Then ah.PrincipalAmount Else 0 End)
From
AccountHistory ah
Where
ah.EntryDate Between '02/10/2021' AND '02/17/2021'
AND ah.AccountNumber Not Between 105 and 135
AND Case When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card Signature'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount < 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card PIN/PINLess'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount > 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - PIN/PINLess'
When ah.TransactionType = 'VCR' AND ah.FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - Debit Card Signature'
When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Debit'
When ah.TransactionType = 'VCR' AND ah. FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Credit'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount < 0 Then 'ATM Withdrawal'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount > 0 Then 'ATM Deposit'
Else 'None'
End <> 'None'
Group By
ah.MemberNumber,
ah.AccountNumber,
Case When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) In (543940,534025) Then 420477
When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) Is Null AND (Select Max(dc.CreditCardAccount) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.BIN = 581551 AND dc.TransactionDate > ah.EntryDate) = 0 Then 581551
When (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber) Is Null OR (Select Max(dc.CreditCardAccount) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.BIN = 420477) <> 0 Then 420477
Else (Select Max(dc.BIN) From DebitCard dc Where dc.MemberNumber = ah.MemberNumber AND dc.CreditCardAccount = ah.AccountNumber)
End,
Case When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card Signature'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount < 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Purchase - Debit Card PIN/PINLess'
When ah.FocusTellerID = 6005 AND ah.PrincipalAmount > 0 AND ah.TokenString Like '%9%875%300%18%POS%' AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - PIN/PINLess'
When ah.TransactionType = 'VCR' AND ah.FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND ah.TokenString Not Like '%pay.fb.com%' AND ah.TokenString Not Like '%VISA%Direct%' AND ah.TokenString Not Like '%Apple%Cash%' Then 'Return - Debit Card Signature'
When ah.TransactionCodeKey = 236 AND ah.PrincipalAmount < 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Debit'
When ah.TransactionType = 'VCR' AND ah. FocusTellerID = 6099 AND ah.PrincipalAmount > 0 AND (ah.TokenString Like '%pay.fb.com%' Or ah.TokenString Like '%VISA%Direct%' OR ah.TokenString Like '%Apple%Cash%') Then 'Money Transfer - Credit'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount < 0 Then 'ATM Withdrawal'
When ah.TokenString Like '%9%875%300%18%ATM%' AND ah.PrincipalAmount > 0 Then 'ATM Deposit'
Else 'None'
End
Order By
ah.MemberNumber
Running the query this way results in the:
Cannot use an aggregate or a subquery in an expression used for the group by list of a GROUP BY clause
If I take out the bold section of the Group By, it results in:
Column 'AccountHistory.EntryDate' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
Finally, if I add the AccountHistory.EndDate to the Group By section, it doesn't group properly.
What can I do to correct this? Thank you.