I have the following database schema. It looks at shoppers and how many orders they have made from three websites in a network.
ID | Name | Country | website1_Orders | website2_Orders | website3_Orders |
---|---|---|---|---|---|
123 | JOHNC | USA | null | 1 | null |
456 | KAYLAB | USA | 5 | null | null |
789 | LAURAT | USA | 2 | 6 | 3 |
999 | RONR | CA | 1 | null | 16 |
017 | MATTE | CA | 7 | null | 4 |
767 | JROB | MX | null | 1 | null |
224 | TINAS | MX | null | null | null |
670 | TOMR | MX | null | 8 | null |
What I want my SQL output to look like is as follows:
Country | Websites_Avail |
---|---|
USA | 3 |
CA | 2 |
MX | 1 |
The logic is that, if no customer in their country has made an order from website1, website2, or website3, then this website does not service that particular country at this time.
So basically, across multiple columns, I need to figure out how to properly aggregate and show the correct number of results, broken out by country. This is a very simple sample of the database - which is much larger.
with count as
(
select
country,
case
when website1_orders is not null
then 'Web 1'
end as Web_One,
case
when website2_orders is not null
then 'Web 2'
end as Web_Two,
case
when website3_orders is not null
then 'Web 3'
end as Web_Three
from
my_database
)
select
country,
COUNT(DISTINCT Web_One) + COUNT(DISTINCT Web_Two) + COUNT(DISTINCT Web_Three) as total_count
from
count
group by
1
This is a very simple version (there are 20 sites in total) and it works for me in theory if I were to just look at these 8ish rows. But it is not scaling and I'm not sure why. I also do not think this is the best way to aggregate across the columns either. But It's all I can think of at this moment.
I would prefer not to do anything like normalizing in a new temp table, but if that's the way to go I'm open to trying to figure out how. But I was hoping within a CTE I could get the correct counts.
Essentially, if a customer in any country makes an order from any site, then 1 is added to the unique total_count
at the end. No state can be more than 20 (which would mean at least one customer from that country has made an order from all of the 20 sites at some point). But I'm getting values into the thousands.
Is there an optimal way of looking at this? It's just Postgres SQL in Snowflake.