JSFiddle - React, Tailwind, and code Playground
by ktabarez
JavaScript
select 'TOTAL SPENT AT STORES'
select highInc.PAYEENAME
,case when RTRIM(highInc.zipcode) = '92081'
then RTRIM(highInc.zipcode) + ' - ' + 'Vista'
else highInc.zipcode
end 'High Income Zip'
,highInc.totalVisits
,highInc.totalSpent
,lowInc.PAYEENAME
,case when RTRIM(lowInc.zipcode) = '92139'
then RTRIM(lowInc.zipcode) + ' - ' + 'South San Diego'
else lowInc.zipcode
end 'Low Income Zip'
,lowInc.totalVisits
,lowInc.totalSpent
from
(
select
lh.PAYEENAME
, n.zipcode
,count(*) 'totalVisits'
,sum(ISNULL(lh.AMOUNT, 0) / 100) 'totalSpent'
from ACCOUNT a
join name n
on a.ACCOUNTNUMBER = n.PARENTACCOUNT
and n.ordinal = 0
and n.zipcode ='92081 '
join LOANHOLD lh
on a.ACCOUNTNUMBER = lh.PARENTACCOUNT
--and lh.PAYEENAME = 'VONS STORE '
group by lh.PAYEENAME
, n.zipcode
) highInc
RIGHT join (
select
lh.PAYEENAME
, n.zipcode
,count(*) 'totalVisits'
,sum(ISNULL(lh.AMOUNT, 0) / 100) 'totalSpent'
from ACCOUNT a
join name n
on a.ACCOUNTNUMBER = n.PARENTACCOUNT
and n.ordinal = 0
and n.ZIPCODE in
(
'92139 '
)
join LOANHOLD lh
on a.ACCOUNTNUMBER = lh.PARENTACCOUNT
--and lh.PAYEENAME = 'VONS STORE '
group by lh.PAYEENAME
, n.zipcode
) lowInc
on highInc.PAYEENAME = lowInc.PAYEENAME
group by
highInc.PAYEENAME
,highInc.zipcode
,highInc.totalVisits
,highInc.totalSpent
,lowInc.PAYEENAME
,lowInc.zipcode
,lowInc.totalVisits
,lowInc.totalSpent
--having highInc.totalSpent < lowInc.totalSpent
order by 3 desc--lowInc.totalVisits desc
select 'TOTAL SPENT'
select n.zipcode
,count(*) 'totalVisits'
,sum(ISNULL(lh.AMOUNT, 0) / 100) 'totalSpent'
from ACCOUNT a
join name n
on a.ACCOUNTNUMBER = n.PARENTACCOUNT
and n.ordinal = 0
and n.zipcode IN
('92081 '
,'92139 ')
join LOANHOLD lh
on a.ACCOUNTNUMBER = lh.PARENTACCOUNT
--and lh.PAYEENAME LIKE '%STARBUCKS%'
group by n.zipcode