I have a task where for each store location I have, I need to get the total number of customers within 5 miles as well as the total number of customers that have a specific status from those results. So say there are 850 customers within the area of store #1. I would also like to know out of all those customers how many have a status of 'Current', 'Inactive', or 'Active'. My result should look something like this:
| Store | Total Customers | Current | Inactive | Active |
| Store 1| 850 | 300 | 50 | 200 |
My customers have a CustomerID, Longitude, Latitude, Status, as well as other columns.
My stores have a StoreID, Longitude, Latitude, and other columns that don't pertain to this example
This is my current query that gets the total number of customers within 5 miles of each one of our stores.
SELECT s.StoreID
, sum(( 3959 * acos( cos( radians(s.Latitude) ) * cos( radians( c.Lat ) )
* cos( radians( c.Lng ) - radians(s.Longitude) ) + sin( radians(s.Latitude) ) * sin(radians(c.Lat)) ) ) <= 5) AS customers
from ata_console.Stores s
cross
join ata_console.Customers c
group
by s.StoreID;
I can't find anything online that relates to a query such as this, because I feel like it's so unique to my situation. If anyone could help me figure out how to modify my current query to what I'm looking for then that would be great, TIA!