Hi all
Here's the tables:
stats_activeemployees
| id | lname | fname |
stats_customer
| id | login |
stats_custcredit
| cust_id | amount |
stats_custredeem
| cust_id | amount |
relationships:
stats_activeemployees.id = stats_customer.login
stats_customer.id = stats_custcredit.cust_id
stats_customer.id = stats_custredeem.cust_id
Now I basically want to return a query that looks like
| employee_id | sum(stats_custcredit amount) - sum(stats_custredeem amount) |
I tried the following ...
... which semi works. Some totals are correct while others are way off. I noticed that if the employee only has a single row in both stats_custcredit and stats_custredeem, the total comes out accurate. Other than that it is inaccurate.
I'm using mysql4
Thanks in advance.
Here's the tables:
stats_activeemployees
| id | lname | fname |
stats_customer
| id | login |
stats_custcredit
| cust_id | amount |
stats_custredeem
| cust_id | amount |
relationships:
stats_activeemployees.id = stats_customer.login
stats_customer.id = stats_custcredit.cust_id
stats_customer.id = stats_custredeem.cust_id
Now I basically want to return a query that looks like
| employee_id | sum(stats_custcredit amount) - sum(stats_custredeem amount) |
I tried the following ...
Code:
select ae.id as empid, c.id, sum(cc.amount) - sum(cr.amount) as voucher
from stats_activeemployees as ae, stats_customer as c, stats_custcredit as cc, stats_custredeem as cr
where ae.id = c.login
and c.id = cc.cust_id
and c.id = cr.cust_id
group by ae.id
I'm using mysql4
Thanks in advance.