sqlite - Complex SQL Query - nested query -
here job_form table...
job_num | name | address | ---------+------+---------+ 1 | tom | smith | 2 | john | doe | 3 | max | smith |
here individual_job table...
job_num | date | description | ---------+------+---------------------+ 1 | 23-01-2012 | eat food | 1 | 24-01-2012 | dishes | 1 | 25-01-2012 | sweep floor | ... | ... | ... | 2 | 19-05-2013 | play games | 2 | 23-05-2013 | code | 2 | 27-05-2013 | sleep | ... | ... | ... | 3 | 23-05-2013 | eat food | 3 | 24-05-2013 | dishes | 3 | 25-05-2013 | sweep floor | ... | ... | ... |
i create query pulls out single row each job_form includes date of first job completed, date of last job completed total number of jobs listed on form. query needs display job forms have jobs need completed in 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 few years, , 1 has me stumped. know have nested query, can't work out order...
any appreciated.
updated include name column in result (forgot this, sorry)
edit per gordon, there no need join if don't require of job_form
specific fields. however, if (e.g. name or address), then:
select jf.job_id, jf.name, min(ij.date) first_job_date, max(ij.date) last_job_date, count(*) count_of_jobs job_form jf inner join individual_job ij on jf.job_num = ij.job_num group jf.job_id, jf.name -- i.e. non-aggregated select fields here
Comments
Post a Comment