Hi @Kevin Lyons ,
Welcome to Microsoft Q&A!
What is your expected output?
Please refer below and check whether it is helpful to you.
;with cte as (
select a.week,a.Price restaurant,b.Price supermarket,c.Allowance
from TableA a
inner join tableB b
on a.Week=b.Week
cross join TableC c)
,cte1 as (
SELECT week,iif(restaurant<Allowance,restaurant,null) restaurant,
iif(supermarket<Allowance,supermarket,null) supermarket
FROM cte)
select week,count(iif(restaurant<supermarket,null,restaurant)) restaurant,
count(iif(restaurant<supermarket,supermarket,null)) supermarket
from cte1
group by week
Output:
week restaurant supermarket
1 1 1
2 1 0
Best regards,
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.