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 2005 Forums
 Transact-SQL (2005)
 Merged resultset with a dynamic field

Author  Topic 

akashenk
Posting Yak Master

111 Posts

Posted - 2009-06-18 : 13:57:19
I have two temp tables (as part of an intermediary procedure) that have the same structure (Table A and Table B).

Table A: EmployeeId, HiredDate
Table B: EmployeeId, HiredDate

Table A contains all possible records, Table B contains a subset of Table A. In other words, a record may exist in both Table A snd B, but ther may be some records which exist in Table A, but not Table B.

I can retrieve both of these result sets of data and process them so that I know which records are in both Table A and B, and which are only in Table A, however, I wonder if there is a more efficient way of doing this using T-SQL so that I don't have to retrieve two result sets with so many duplicate records. Is there a way to retrieve a "merged" resultset through a Join or some other operation, and create a field which designates that the record exists in Table A, but not Table B?

So for example, the retrieved resultset would have EmployeeId, HiredDate and some additional field "FoundInB", or whatever which designates whether the record exists in Table B or not.

Thanks in advance!

vijayisonly
Master Smack Fu Yak Hacker

1836 Posts

Posted - 2009-06-18 : 14:07:30
quote:
Originally posted by akashenk

I have two temp tables (as part of an intermediary procedure) that have the same structure (Table A and Table B).

Table A: EmployeeId, HiredDate
Table B: EmployeeId, HiredDate

Table A contains all possible records, Table B contains a subset of Table A. In other words, a record may exist in both Table A snd B, but ther may be some records which exist in Table A, but not Table B.

I can retrieve both of these result sets of data and process them so that I know which records are in both Table A and B, and which are only in Table A, however, I wonder if there is a more efficient way of doing this using T-SQL so that I don't have to retrieve two result sets with so many duplicate records. Is there a way to retrieve a "merged" resultset through a Join or some other operation, and create a field which designates that the record exists in Table A, but not Table B?

So for example, the retrieved resultset would have EmployeeId, HiredDate and some additional field "FoundInB", or whatever which designates whether the record exists in Table B or not.

Thanks in advance!



To get all employees existing in A and also in B
select a.EmployeeId, a.HiredDate, 'Y' as Found_Ind
from TableA a inner join TableB b
on a.EmployeeId = b.EmployeeId

To get all employees existing in A but not in B
select a.EmployeeId, a.HiredDate, 'Y' as Not_Found_Ind
from TableA a left join TableB b
on a.EmployeeId = b.EmployeeId
where b.EmployeeId is null

Go to Top of Page

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2009-06-18 : 14:18:02
select a.EmployeeId
, a.HiredDate
,
,[Found_Ind] = case when b.employeeid is not null then 'Y' else 'N' end)
from TableA a
left join TableB b
on a.EmployeeId = b.EmployeeId

Jim
Go to Top of Page

akashenk
Posting Yak Master

111 Posts

Posted - 2009-06-18 : 14:36:46
Thanks! That worked great!
Go to Top of Page
   

- Advertisement -