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 2012 Forums
 Transact-SQL (2012)
 Simple Query

Author  Topic 

wided
Posting Yak Master

218 Posts

Posted - 2014-11-19 : 10:56:08


table1 header:

col1 col2 col3 col4 col5
5 2014 2014-08-29 00:00:00.000 AB_TND 2014/0636
5 2014 2014-08-07 00:00:00.000 STUSID_USD 2014/0641

Table2: details

col1 col2 col3 col4
5 1 627801 627801
5 2 532103 532103
5 1 99 99
5 2 532405 532405

my query

Select col1, col2, col3, col4
table1 INNER JOIN table2
ON table1.col1 = table2.col1
where table1.col4 = 'STUSID_USD'


the result is this:

5 1 627801 627801
5 2 532103 532103
5 1 99 99
5 2 532405 532405

i want to have this result:

5 1 99 99
5 2 532405 532405

what is the problem?

thanks

gbritton
Master Smack Fu Yak Hacker

2780 Posts

Posted - 2014-11-19 : 12:49:35
You're joining on col1, which only has one value -- 5 -- in both tables, so all 4 rows are returned. Why do you think that the two rows you don't want should be excluded? If they were, SQL would have a bug!

Put another way, what value in table1 should be used to restrict the rows retrieved in table2? Note that table.col4 won't do, since there is no column in table 2 with that value in any row.
Go to Top of Page
   

- Advertisement -