I have three MySQL Tables: customers, persons, and dates_d
customers
and persons
are related through agent_code
dates_d is just a table with all the dates of the last and following 10 years.
that key allows me to determine how many customers each person has entered. I also have another key with the date a customer was entered, therefore I can query the database and see how many customers have been entered each Month, Year, Week, etc...
My current sample of the database contains records from last August, if I run the following query:
SELECT COUNT(customers.hash) FROM customers
LEFT JOIN persons ON persons.agent_code
WHERE customers.agent_code = persons.agent_code
GROUP BY customers.agent_code
it will tell me the amount of customers each agent has. but I need to know how many customers, every agent entered the current week, and grouped by day.
I know is not that hard, and I more or less thought of a way to make it work:
- join dates_d and customers table
- join customers table and persons table.
- Filter the information by date and group them by person, by day.
So I can display it as:
Thomas, Bryan: Monday 17: 20 customers Tuesday 18: 12 customers. Wednesday 19: 3 customers. Thursday 20: 0 customers
and so on.
yet again, I've been trying different queries, but I haven't been able to come up with the info I need out of the database. I'm not asking for the specific query, but any help that points me to the right direction will be greatly appreciated!
Thanks!
EDIT 1: Sample Data:
CREATE TABLE IF NOT EXISTS `customers` (
`hash` varchar(32) NOT NULL,
`date_joined` date NOT NULL,
`agent_code` int(5) NOT NULL DEFAULT '0',
PRIMARY KEY (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `customers` (`hash`, `date_joined`, `agent_code`) VALUES
('0323619e9dd37726ad9aede6b8941022', '2012-09-17', 20004),
('0a5a74acc39773c191b87b759799b0c0', '2012-08-02', 22109),
('1aa4d97ba79dce047d519efe3832b5e5', '2012-07-19', 22109),
('2605578b2e35f01f473591d8f3ed3c51', '2012-08-06', 20003),
('26ce0904a6ea30da9b181a301937664e', '2012-07-30', 20003);
CREATE TABLE IF NOT EXISTS `persons` (
`agent_code` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`agent_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `persons` (`agent_code`) VALUES
(20003),(20004);