alohaaaron
Programmer
Hi, I'd like the results of a SQL query to look like where I am totaling individual results as well as state results. I am able to total individual results but not total state results. The query is at the bottom of the text.
In addition I have a lookup table with CustomerName and StateName columns. I want to use this as a lookup table. The lookup table contains all the CustomerName's and all StateName's but other fields are empty placeholders so I can do a union. If there is a missing customer that is supposed to be in the original 'tracking' table (left table) if I do a union with the lookup table (right table) It also includes the lookup table entries even if they already exist in the left table. I only want to select the entries in the right table if it doesn't exist in the left table.
The table structure looks like this.
CustomerName (varchar 20), StateName (varchar 2), Status (varchar 10)
The results should look like this.
CustomerName, StateName, Total Customers YES, NO
Fred WA 3 3 0 <-- individual total
Bob WA 2 1 1 <-- individual total
Max WA 1 1 0 <-- individual total
Totals: WA 6 4 1 <-- state total
Ken IL 3 3 0 <-- individual total
Joe IL 2 1 1 <-- individual total
Mark IL 1 1 0 <-- individual total
Totals: WA 6 4 1 <-- state total
SELECT
CustomerName, StateName
Count(CustomerName) as "Total Customers",
SUM(IF(Status='YES',1,0)) as YES,
SUM(IF(Status='NO',1,0)) as NO,
FROM tracking
GROUP BY CustomerName Having StateName = 'WA';
In addition I have a lookup table with CustomerName and StateName columns. I want to use this as a lookup table. The lookup table contains all the CustomerName's and all StateName's but other fields are empty placeholders so I can do a union. If there is a missing customer that is supposed to be in the original 'tracking' table (left table) if I do a union with the lookup table (right table) It also includes the lookup table entries even if they already exist in the left table. I only want to select the entries in the right table if it doesn't exist in the left table.
The table structure looks like this.
CustomerName (varchar 20), StateName (varchar 2), Status (varchar 10)
The results should look like this.
CustomerName, StateName, Total Customers YES, NO
Fred WA 3 3 0 <-- individual total
Bob WA 2 1 1 <-- individual total
Max WA 1 1 0 <-- individual total
Totals: WA 6 4 1 <-- state total
Ken IL 3 3 0 <-- individual total
Joe IL 2 1 1 <-- individual total
Mark IL 1 1 0 <-- individual total
Totals: WA 6 4 1 <-- state total
SELECT
CustomerName, StateName
Count(CustomerName) as "Total Customers",
SUM(IF(Status='YES',1,0)) as YES,
SUM(IF(Status='NO',1,0)) as NO,
FROM tracking
GROUP BY CustomerName Having StateName = 'WA';