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.
| 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, HiredDateTable B: EmployeeId, HiredDateTable 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, HiredDateTable B: EmployeeId, HiredDateTable 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 Bselect a.EmployeeId, a.HiredDate, 'Y' as Found_Indfrom TableA a inner join TableB bon a.EmployeeId = b.EmployeeIdTo get all employees existing in A but not in Bselect a.EmployeeId, a.HiredDate, 'Y' as Not_Found_Indfrom TableA a left join TableB bon a.EmployeeId = b.EmployeeIdwhere b.EmployeeId is null |
 |
|
|
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 bon a.EmployeeId = b.EmployeeIdJim |
 |
|
|
akashenk
Posting Yak Master
111 Posts |
Posted - 2009-06-18 : 14:36:46
|
| Thanks! That worked great! |
 |
|
|
|
|
|
|
|