Home All Groups Group Topic Archive Search About

Obtain values from different tables

Author
6 Jun 2006 2:01 PM
hugonsantos
Table processes_user
id_user | id_proc
-------------------------
35        | 17001
100      | 1089
35        | 17002


Table processes_flow
(example:the process 17001 is with the user 35 and the deadline of the
flow is 2006-07-30! As you can see there are 2 entries on the table
below, the first with a lower id references an old flow, but I want to
get the deadline of the last flow of the process)
-----------------------------------
id | id_proc | deadline
-----------------------------------
10| 17001   | null
12| 1089     | 2006-05-12
15| 17001   | 2006-07-30
20| 17002   | null
-----------------------------------

I would like to get for the user 35 the following info:
17001 | 2006-07-30
17002 | null

How can I do this with a sql command? I would like you to share some
ideas because I'm stuck with this.

Regards,
Hugo Santos

Author
6 Jun 2006 2:07 PM
Stu
SELECT a.id_user, a.id_proc, b.deadline
FROM processes_user a
LEFT JOIN (SELECT id_proc, MAX(deadline) as deadline
                  FROM processes_flow
                  GROUP BY id_proc) b ON a.id_proc = b.id_proc
WHERE a.id_user= 35


Untested.

Stu

hugonsan***@gmail.com wrote:
Show quoteHide quote
> Table processes_user
> id_user | id_proc
> -------------------------
> 35        | 17001
> 100      | 1089
> 35        | 17002
>
>
> Table processes_flow
> (example:the process 17001 is with the user 35 and the deadline of the
> flow is 2006-07-30! As you can see there are 2 entries on the table
> below, the first with a lower id references an old flow, but I want to
> get the deadline of the last flow of the process)
> -----------------------------------
> id | id_proc | deadline
> -----------------------------------
> 10| 17001   | null
> 12| 1089     | 2006-05-12
> 15| 17001   | 2006-07-30
> 20| 17002   | null
> -----------------------------------
>
> I would like to get for the user 35 the following info:
> 17001 | 2006-07-30
> 17002 | null
>
> How can I do this with a sql command? I would like you to share some
> ideas because I'm stuck with this.
>
> Regards,
> Hugo Santos
Are all your drivers up to date? click for free checkup

Author
6 Jun 2006 2:41 PM
hugos
Stu wrote:
> SELECT a.id_user, a.id_proc, b.deadline
> FROM processes_user a
> LEFT JOIN (SELECT id_proc, MAX(deadline) as deadline
>                   FROM processes_flow
>                   GROUP BY id_proc) b ON a.id_proc = b.id_proc
> WHERE a.id_user= 35
>
>
> Untested.
>
> Stu
>

Thanks for your reply Stu, but with that aren't you going to get the
max deadline only? I want to show the deadline from the last flow.
For example.. on the first flow you may have a deadline and on the
second the deadline is null. And I want to get the deadline from the
last flow... which is null.
Can you clear this out?
Author
6 Jun 2006 4:32 PM
Stu
I'm sorry, I made an assumption that may or may not be true; I'm
assuming that NULL comes before a deadline.  In that case, the subquery
will only return rows that have a deadline associated with them; doing
a LEFT JOIN will return the MAX(deadline) if there is one, and NULL if
there is not.


hugos wrote:
Show quoteHide quote
> Stu wrote:
> > SELECT a.id_user, a.id_proc, b.deadline
> > FROM processes_user a
> > LEFT JOIN (SELECT id_proc, MAX(deadline) as deadline
> >                   FROM processes_flow
> >                   GROUP BY id_proc) b ON a.id_proc = b.id_proc
> > WHERE a.id_user= 35
> >
> >
> > Untested.
> >
> > Stu
> >
>
> Thanks for your reply Stu, but with that aren't you going to get the
> max deadline only? I want to show the deadline from the last flow.
> For example.. on the first flow you may have a deadline and on the
> second the deadline is null. And I want to get the deadline from the
> last flow... which is null.
> Can you clear this out?
Author
6 Jun 2006 7:21 PM
hugos
Thanks.
I already found a different way! I think it's not optimized but it will
work for now ;)

Stu escreveu:
Show quoteHide quote
> I'm sorry, I made an assumption that may or may not be true; I'm
> assuming that NULL comes before a deadline.  In that case, the subquery
> will only return rows that have a deadline associated with them; doing
> a LEFT JOIN will return the MAX(deadline) if there is one, and NULL if
> there is not.

Bookmark and Share