Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select query with left outer join

Author  Topic 

cplusplus
Aged Yak Warrior

567 Posts

Posted - 2011-03-29 : 16:58:08
I am using the following select query, but even though i put element_id=10, i am getting id "1" rows also along with 10.

select t.element_id,t.taskid,t.award_amt, c.amount from tab_tasks t left outer join TAB_CostDetails c on (t.taskid = c.taskid and c.EstType ='Fin' and c.COexec = 1 and t.element_id=10)

Thank you very much for the helpful info.


yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2011-03-29 : 17:52:28
what happens with this?


select t.element_id,t.taskid,t.award_amt, c.amount
from tab_tasks t
left outer join TAB_CostDetails c
on t.taskid = c.taskid
WHERE c.EstType ='Fin'
and c.COexec = 1
and t.element_id=10


If you don't have the passion to help people, you have no passion
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-29 : 17:55:20
The way you have written it, the t.element_id = 10 applies ONLY to the join condition. If you want only element_id=10, put it in the where clause as in
select t.element_id,t.taskid,t.award_amt, c.amount from tab_tasks t left outer join TAB_CostDetails c on (t.taskid = c.taskid and c.EstType ='Fin' and c.COexec = 1 and t.element_id=10)
where t.element_id=10

Depending on whether or not you want to join only when t.element_id is 10, you may want to keep or remove the condition on t.element from the join, but with the where condition written as it is now that does not make any difference.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-03-29 : 17:56:17
I did it again :--)

[Resolves to herself to check if someone has responded before posting a reply]
Go to Top of Page
   

- Advertisement -