Here is the job_form table...
job_num | name | address |
---------+------+---------+
1 | Tom | Smith |
2 | John | Doe |
3 | Max | Smith |
Here is the individual_job table...
job_num | date | description |
---------+------+---------------------+
1 | 23-01-2012 | Eat food |
1 | 24-01-2012 | Do dishes |
1 | 25-01-2012 | Sweep floor |
... | ... | ... |
2 | 19-05-2013 | Play games |
2 | 23-05-2013 | Do code |
2 | 27-05-2013 | Sleep |
... | ... | ... |
3 | 23-05-2013 | Eat food |
3 | 24-05-2013 | Do dishes |
3 | 25-05-2013 | Sweep floor |
... | ... | ... |
I would like to create a query that pulls out a single row for each job_form which includes the date of the first job to be completed, the date of the last job to be completed as well as the total number of jobs listed on the form. The query needs to display only job forms which have jobs which need to be completed in the future.
Example is:
job_num | first_job_date | last_job_date | count_of_jobs | name
---------+------------------+-----------------+-----------------+-------------
2 | 19-05-2013 | 27-05-2013 | 3 | John
3 | 23-05-2013 | 25-05-2013 | 3 | Max
I haven't done SQL for a few years, and this one has me completely stumped. I know I have to do a nested query, but can't work out the order...
Any help much appreciated.
Updated to include name column in result (forgot about this, sorry)