create table expected_sales
(
sales_rep id integer not null,
month integer not null,
expected_sales_count integer not null,
primary key(sales_rep_id, month)
);
create table sales
(
sales_id integer unsigned not null auto_increment,
sales_rep_id integer not null,
sales_date datetime,
sales_total decimal(10),
primary key (sales_id)
);
create table sales_details
(
sales_id integer unsigned not null,
product_id integer not null,
count integer,
unit_price decimal
);
SELECT s.sales_rep_id, sum(sd.count) as total_items
FROM sales s LEFT JOIN sales_details sd
ON s.sales_id = sd.sales_id
WHERE MONTH(sales_date) = $month
GROUP BY s.sales_rep_id
That query should give you how many items were sold by each sales rep.
You can add the third table (expected sales)
to the join and then calculate the difference afterwards.
HTH
-Gio-