Hopefully I can do the problem justice, because it was too difficult to summarise it in the title! (suggestions are welcome in the comments)
Right, so here's my table:
Tasks
task_id (number)
job_id (number)
to_do_by_date (date)
task_name (varchar / text)
status (number)
completed_date (date)
for arguments sake let's make the values of status:
1 = New
2 = InProgress
3 = Done
and what I'm having trouble trying to do is create a query that pulls back all of the tasks:
- where any of the tasks for a
job_id
have astatus
<> Done- except where all tasks for a
job_id
are are done, but one or more tasks have acompleted_date
of today
- except where all tasks for a
- ordered by the
to_be_done_by
date, but grouping all of the job_id tasks together- so the
job_id
with the next `to_do_by_date' task is shown first
- so the
some information about the data:
- a
job_id
can have an arbitrary number of tasks
Here's an example of the output I'm trying to get:
task_id job_id to_do_by_date task_name status completed_date
1 1 yesterday - 3 yesterday
2 1 today - 3 today
3 2 now - 3 today
4 2 2 hours time - 2 {null}
5 2 4 hours time - 2 {null}
6 2 tomorrow - 1 {null}
7 3 3 hours time - 2 {null}
8 3 tomorrow - 1 {null}
9 3 tomorrow - 1 {null}
I'm using Oracle 10g, so answers for Oracle or ANSI SQL, or a hint for how to approach this would be ideal, and I can create Views or wrap this in a Stored Procedure to offload logic from the application if your solution calls for it.
here's a sql script that will create the example test data shown above:
create table tasks (task_id number, job_id number, to_do_by_date date, task_name varchar2(50), status number, completed_date date);
insert into tasks values (0,0,sysdate -2, 'Job 0, Task 1 - dont return!', 3, sysdate -2);
insert into tasks values (1,1,sysdate -1, 'Job 1, Task 1', 3, sysdate -1);
insert into tasks values (2,1,sysdate -2/24, 'Job 1, Task 2', 3, sysdate -2/24);
insert into tasks values (3,2,sysdate, 'Job 2, Task 1', 3, sysdate);
insert into tasks values (4,2,sysdate +2/24, 'Job 2, Task 2', 2, null);
insert into tasks values (5,2,sysdate +4/24, 'Job 2, Task 3', 2, null);
insert into tasks values (6,2,sysdate +1, 'Job 2, Task 4', 1, null);
insert into tasks values (7,3,sysdate +3/24, 'Job 3, Task 1', 2, null);
insert into tasks values (8,3,sysdate +1, 'Job 3, Task 2', 1, null);
insert into tasks values (9,3,sysdate +1, 'Job 3, Task 3', 1, null);
commit;
Many, many thanks for your help :o)