I’m writing up a reporting system for a recruitment website I made. So far I’ve managed to get the function below to pull through total jobs over a given time period grouped in days, weeks months or years
What I’d like to do is pull through the total job applications for that same period. The job applications are stored in another table called job_application, and contain job_application_id, job_id and date_added fields
Is there a way I can pull that data through in the same query? The SQL is beginning to fry my brain so any help would be much appreciated!
public function getJobReport($data = array()) {
$sql = "SELECT MIN(tmp.date_added) AS date_start, MAX(tmp.date_added) AS date_end, COUNT(tmp.job_id) AS `jobs` FROM (SELECT j.job_id, j.date_added FROM `" . DB_PREFIX . "job` j WHERE 1";
if (isset($data['date_start']) && $data['date_start']) {
$sql .= " AND DATE(j.date_added) >= '" . $this->db->escape($data['date_start']) . "'";
}
if (isset($data['date_end']) && $data['date_end']) {
$sql .= " AND DATE(j.date_added) <= '" . $this->db->escape($data['date_end']) . "'";
}
$sql .= " GROUP BY j.job_id) tmp";
if (isset($data['group'])) {
$group = $data['group'];
} else {
$group = 'week';
}
switch($group) {
case 'day';
$sql .= " GROUP BY DAY(tmp.date_added)";
break;
default:
case 'week':
$sql .= " GROUP BY WEEK(tmp.date_added)";
break;
case 'month':
$sql .= " GROUP BY MONTH(tmp.date_added)";
break;
case 'year':
$sql .= " GROUP BY YEAR(tmp.date_added)";
break;
}
if (isset($data['start']) || isset($data['limit'])) {
if ($data['start'] < 0) {
$data['start'] = 0;
}
if ($data['limit'] < 1) {
$data['limit'] = 20;
}
$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
}
$query = $this->db->query($sql);
return $query->rows;
}
I don’t readily see why a subquery is necessary here…
This Query uses a left join and selects as columns your input start and end dates, then GROUP’s by that (so there will be exactly one result row). I have skimped a little on the specifics of your identifiers to make this more readable.
EDIT: Note that this technique requires your start and end dates to be set… but I highly suggest you define reasonable defaults anyway. If you want the defaults to be wide, use epoch and +100 years from now, or something… but perhaps something like “this quarter” is more appropriate for your client.
EDIT 2 (adding date resolution): It is important to realize that with this kind of query, you can only get accurate date selection on EITHER jobs or applications, and the other will be “all”. I chose what I think is more sensible, to select on application date and include all jobs no matter when they were posted (IF they have applications submitted during the specified period). If you want the other way (only show jobs posted during the period), you can change the column passed to the date function, and make the JOIN a LEFT join… but keep in mind that the data for number of applications to that job will be for the ENTIRE period, not just the DAY/WEEK the job was posted.