|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Obtain values from different tablesid_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 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 Stu wrote:
> SELECT a.id_user, a.id_proc, b.deadline Thanks for your reply Stu, but with that aren't you going to get the> 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 > 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? 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? 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.
Other interesting topics
Identity or GUID?
Index Tuning Running Sum Query ? How to add separator blank rows by SQL Query? Identity Columns - Design Question User defined fields via application How can I update the col value using extended stored procedure Access "inserted" / "deleted" from stored procedure Dynamic View query plan on sql server 2005 |
|||||||||||||||||||||||