- When an Account is 'PrePaid' (PlanCode = P0100) then the balance owing is in negative (eg; -100 if they are owing $100.
When an Account is 'PostPaid' (PlanCode = P0200) then the balance owing is positive (eg; 100 if they are owing $100)
Some PrePaid accounts are owed money to and hence show balance as positive (eg; 50 would mean that we owe them $50)
- Some PostPaid Accounts are owed money too (as in they overpaid) and show balance as negative (eg; -50 would mean that we owe them $50)
I want to generate a report that shows all accounts that owe us $5 or more.
My code is wrong, but perhaps my logic is right?
SELECT A.[AccountId]
,A.[AccountNumber]
,CASE WHEN P.PlanCode = 'P0100' THEN 'PrePaid'
WHEN P.PlanCode = 'P0200' THEN 'PostPaid'
END PlanType
,CASE WHEN P.PlanCode = 'P0100' THEN (Select ABS(CurrentBalance) FROM Account WHERE CurrentBalance < 0)
WHEN P.PlanCode = 'P0200' THEN (Select [CurrentBalance] FROM Account WHERE CurrentBalance > 0)
END [CurrentBalance]
FROM [GEB_DWH].[dbo].[Account] A
INNER JOIN AccountPlan P ON A.AccountId = P.AccountId
WHERE A.CurrentBalance >= 5
ORDER BY CurrentBalance DESC
EDIT:
I've also tried:
SELECT A.[AccountId]
,A.[AccountNumber]
,CASE WHEN P.PlanCode = 'P0100' THEN 'PrePaid'
WHEN P.PlanCode = 'P0200' THEN 'PostPaid'
END PlanType
,ABS([CurrentBalance])[CurrentBalance]
--,CASE WHEN P.PlanCode = 'P0100' THEN (Select ABS(CurrentBalance) FROM Account WHERE CurrentBalance < 0)
--WHEN P.PlanCode = 'P0200' THEN (Select [CurrentBalance] FROM Account WHERE CurrentBalance > 0)
-- END [CurrentBalance]
FROM [GEB_DWH].[dbo].[Account] A
INNER JOIN AccountPlan P ON A.AccountId = P.AccountId
WHERE ((P.PlanCode = 'P0100' AND (A.CurrentBalance) < 0) OR (P.PlanCode = 'P0200' AND (A.CurrentBalance) > 0))
AND A.CurrentBalance > 5
ORDER BY CurrentBalance DESC