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 |
|
sqldev6363
Yak Posting Veteran
54 Posts |
Posted - 2011-01-20 : 12:47:21
|
| i need a query for this logic..i have 3 tables Tbl1ID KeyID Name Address1 101 Chris 101..2 102 Jay 229..3 103 shan 3129..4 104 Dan 432..5 105 tom 5466 106 Pim 565...7 107 Sam 839.....second table is Tbl2ID LoginID State1 001 NULL2 002 FL3 003 CA 4 NULL NULL7 NULL NULL...Third table is tbl3KEYID LoginID State105 005 TX106 NULL NULL107 007 MD...Logic is first i need to check whether loginID and state are there in tbl2 with the tbl1 using ID column and does not exists in tbl2 then i need to check in tbl3 with tbl1 using keyID column and the output i need in one table like thistbl4ID HasLoginID State1 Y NULL2 Y FL3 Y CA4 N NULL5 Y TX6 N NULL7 Y MD....Can i get query for this logic...thanks in advancedev |
|
|
nigelrivett
Master Smack Fu Yak Hacker
3385 Posts |
Posted - 2011-01-20 : 12:54:27
|
| select t1.id, hasloginid = case when coalesce(t2.loginid,t3.loginid) is not null then 'Y' else 'N' end, State = coalesce(t2.state,t3.state)from tbl1 t1left join tbl2 t2on t1.id = t2.idleft join tbl3 t3on t1.keyid = t3.keyid==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy. |
 |
|
|
sqldev6363
Yak Posting Veteran
54 Posts |
Posted - 2011-01-20 : 16:11:30
|
THIS IS GOOD BUT THE PROBLEM IS I HAVE DUPLICATE RECORDS IN THE TABLE..THIS IS THE EXAMPLE OF THE OUTPUT I AM GETTINGID HASLOGINID STATE1 Y NULL1 Y TX2 Y CA2 Y NULL3 N NULL4 Y VA5 N NULL6 Y NM6 Y NULL7 Y NULLI NEED THE OUTPUT LIKE THISID HASLOGINID STATE1 Y TX2 Y CA3 N NULL4 Y VA5 N NULL6 Y NM7 Y NULLHOW TO FILTER LIKE THIS FROM THAT OUPUTCAN YOU PLEASE HELP ME IN THIS..THANKS IN ADVANCEquote: Originally posted by nigelrivett select t1.id, hasloginid = case when coalesce(t2.loginid,t3.loginid) is not null then 'Y' else 'N' end, State = coalesce(t2.state,t3.state)from tbl1 t1left join tbl2 t2on t1.id = t2.idleft join tbl3 t3on t1.keyid = t3.keyid==========================================Cursors are useful if you don't know sql.SSIS can be used in a similar way.Beer is not cold and it isn't fizzy.
dev |
 |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2011-01-21 : 00:23:05
|
| If you dont need the Null Values append a check to the above mentioned query:Where (tbl2.STATE is not null and tbl3.STATE is not null) |
 |
|
|
sqldev6363
Yak Posting Veteran
54 Posts |
Posted - 2011-01-21 : 08:07:14
|
I need the NULL values also but the condition is i have show atleast one ID which is having the "Y" and "State" and having IDs with only "Y" and State is "NULL" and having IDs HasKeyID = NULL and State = NULL.If you see the sample output results that i posted previously you will get an idea how i need the output.can any one help me on thisquote: Originally posted by MIK_2008 If you dont need the Null Values append a check to the above mentioned query:Where (tbl2.STATE is not null and tbl3.STATE is not null)
dev |
 |
|
|
|
|
|