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
 General SQL Server Forums
 New to SQL Server Programming
 Conditional Join

Author  Topic 

baja_yu
Starting Member

24 Posts

Posted - 2010-08-16 : 12:38:41
Hi people,

I'm not sure if the terminology in the thread title is right, but here is what I'm trying to do. Lets say I have two tables:

tbl1: Name, ID
tbl2: tbl1ID, ID

Table 2 is a list of IDs from the first table. What I would like to show as a result is a table looking like this:

Name | Comment | ID
---------------------------
name1 | | 1
name2 | | 2
name3 | aaaaa | 3

The comment column would be empty if the ID from tbl1 does not exist in tbl2, and it would read anything if the record with that ID does exist in tbl2. The end result is basically the entire tbl1 listed with an additional column to indicate which item is in tbl2 too.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-16 : 12:43:06
[code]
SELECT t1.Name,CASE WHEN t2.ID IS NULL THEN '' ELSE 'aaaaa' END AS Comment,t1.ID
FROM tbl1 t1
LEFT JOIN tbl2 t2
ON t1.ID=t2.tbl1ID
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-16 : 13:39:41
ELSE 'aaaaa'

???

Really?




Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

baja_yu
Starting Member

24 Posts

Posted - 2010-08-16 : 16:05:18
visakh16, thanks for the reply, I'll give it a try and report back.

X002548, what did you mean by your comment? Do you think the method visakh16 suggested wont work, or do you think my method looks odd? I am doing this as I need to manually check a table with all the records in it, and pay special attention to the ones with IDs in table 2, so I just need to mark them in any way. And I need to check them in context with other elements of table1. With the additional column and anything written in it for items that are in table2 also, it will be easy for me to spot them without manually comparing IDs or what not.
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-16 : 16:20:11
Mark them?

How about ONLY DISPLAY the outliers

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page

baja_yu
Starting Member

24 Posts

Posted - 2010-08-16 : 16:37:35
Table2 two contains a list of IDs to items in table1, I need to list the entire table1 so I can inspect the records, but I need to pay special attention to ones with IDs in table2. That new column is just to point them out to me visually.

Lets say I have in table1

Name | ID
---------
Mark | 1
Peter | 2
John | 3
Jack | 4
Michael | 5

And in table2 I have

tbl1ID | ID
-----------
3 | 1

What I wanted to do is get a result like this

Name | Comment | ID
--------------------
Mark | | 1
Peter | | 2
John | aaaaaa | 3
Jack | | 4
Michael | | 5

That way when I'm looking through all of them I see that I need to pay attention to John. Table1 can have thousands of items. I hope that clears it. That's just some example data, the tables have a lot more fields, and I need to list all the items from table1 for my inspection, not just the ones referred in table 2.
Go to Top of Page

baja_yu
Starting Member

24 Posts

Posted - 2010-08-16 : 20:53:41
visakh16, works like a charm! Many thanks!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-08-17 : 11:02:45
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2010-08-17 : 12:02:10
why wouldn't you just do


SELECT *
FROM tbl1 t1
INNER JOIN tbl2 t2
ON t1.ID=t2.tbl1ID



??????

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Add yourself!
http://www.frappr.com/sqlteam



Go to Top of Page
   

- Advertisement -