Hope you are all doing good and safe.
I am working on a reporting requirement to get the count of all approved, declined and referred deals from the system.
i.e. count of all approved, declined and referred deals for a given period.
The table (decision) attached has the data of all decisions.
The expected report for the data attached:
Approved: Deals approved without refer or decline directly.
Declined: Deals declined directly without refer.
Refer: Deals referred at least once.
Refer later approved: Referred deals later approved.
Refer later declined: Referred deals later declined.
The sql which I use to get the data for approved and declined. I fetch the data for both approved and declined and later apply filter in tableau to get the count.
select nt.*
from (
SELECT deal, COUNT(deal) AS countOfCt
FROM decision
where subject like '%Credit Decision%'
and comments like '%DecisionCredit%'
and activity_dt between '2021-06-01' and '2021-06-30'
GROUP BY deal
) as nt2,
note nt
where nt.deal=nt2.deal
and nt2.countOfCt=1
and subject like '%Credit Decision%'
and comments like '%DecisionCredit%'
and activity_dt between '2021-06-01' and '2021-06-30'
order by nt.deal, nt.activity_dt
I came across a scenario (deal: 12) where decision triggered more than once and both the time it got approved.
I am struggling to amend the sql to cover this particular scenario. Can you check and suggest?
Note: I am planning to use a separate custom sql to get referred deals.
Thanks in advance.
EDIT: UPDATED:
As there are restrictions in using parameters with common table expressions in tableau, Is there a way to create the custom sql without CTE's?.