I have a table of customer data. I will be joining it to a location table. Customer ID is distinct but Location ID is not because multiple customers can belong to one location. Each customer is identified as having an "Active" or "Inactive" status. Each customer is also identified as whether he or she is the location's Billing Contact.
I need to find all the Location ID values where at least one of these three conditions is true:
- No customer is listed as the Billing Contact.
- All customers listed as the Billing Contact have an "Inactive" status.
- All customers have an "Inactive" status.
I tried something like the query below (the examples have been simplified), and I know it can't be right, but I can't see exactly how because I get an error message about invalid columns "in the HAVING clause because it is not ctonained in either an aggregate function or the GROUP BY clause." I can put those two additional columns into the GROUP BY clause, but then I'm doing three groups when I'm pretty sure I only want to do one group.
SELECT DISTINCT
vLocation.LocationName
,vLocation.LocationID
,vCustomer.LocationID
,vCustomer.CustomerID
,vCustomer.CustomerName
,vCustomer.Status
,vCustomer.BillingContact
,vCustomer.Email
,vCustomer.Status
FROM
DB.vLocation
LEFT OUTER JOIN DB.vCustomer
ON vLocation.LocationID = vCustomer.LocationID
WHERE
vLocation.Status = 'Active'
AND (vCustomer.CustomerID IS NULL
OR vCustomer.CustomerID IN (
SELECT DISTINCT
vCustomer.CustomerID
FROM
DB.vCustomer
GROUP BY
vCustomer.CustomerID
HAVING
vCustomer.BillingContact = 'False'
OR (vCustomer.BillingContact = 'True'
AND vCustomer.Status = 'Inactive')
OR vCustomer.Status = 'Inactive'
)
)
A coworker suggested asking an AI for help, and it suggested this, which allowed me to get data, but my results were wrong because locations with active billing contacts were included.
HAVING
SUM(CASE WHEN BillingContact = 'False' THEN 1 ELSE 0 END) > 0
OR
SUM(CASE WHEN BillingContact = 'True' AND Status = 'Inactive' THEN 1 ELSE 0 END) > 0
Here is a sample table. Location ID 004 has no active customers and should be included in the results. Same with Location ID 085 even though one of the customers is the Billing Contact. Location ID 091 has an active customer who is not the Billing Contact and an inactive customer who is the Billing Contact. This Location ID should be included in the results. Location ID 092 has two active customers but neither one is the Billing Contact; it should be included in the results. Location IDs 100 and 101 should be entirely EXCLUDED from the results because AT LEAST ONE of them is active and is the Billing Contact.
| Location ID | Customer ID | Customer Name | Status | Billing Contact |
|---|---|---|---|---|
| 004 | 003 | Bri | Inactive | FALSE |
| 004 | 004 | Rac | Inactive | FALSE |
| 004 | 005 | Tam | Inactive | FALSE |
| 085 | 157 | Ari | Inactive | FALSE |
| 085 | 158 | Hel | Inactive | TRUE |
| 085 | 159 | Ran | Inactive | FALSE |
| 091 | 167 | Jud | Inactive | TRUE |
| 091 | 168 | Ste | Active | FALSE |
| 092 | 169 | Izz | Active | FALSE |
| 092 | 170 | Kur | Active | FALSE |
| 100 | 183 | Ash | Active | FALSE |
| 100 | 184 | Bre | Active | TRUE |
| 101 | 185 | Cec | Active | FALSE |
| 101 | 186 | Emi | Active | FALSE |
| 101 | 187 | Eri | Active | TRUE |
| 101 | 188 | Jen | Active | FALSE |
| 101 | 189 | Jud | Inactive | TRUE |
| 101 | 190 | Mel | Active | FALSE |
SELECT DISTINCTyou are very likely wrong.SELECT DISTINCTis very rarely needed and almost always an indicator for an inappropriate query.