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 |
|
nbalraj
Starting Member
21 Posts |
Posted - 2009-04-16 : 09:19:59
|
| Hi,I have these 2 tables:table1cntTable1 cntTable2 ysnaward1 NULL 02 NULL 03 NULL 04 4 1cntTable2 is the primarykey in table2table 2cntTable2 strTable2 ysnapproved1 sampledesc1 02 sampledesc2 13 sampledesc3 14 sampledesc4 1Need help in selecting all the records from table2, but need to avoid records, for which the table1 has ysnaward = 1 and in table2 ysnaward = 1the end result should be like thiscntTable2 strTable2 ysnapproved ysnaward2 sampledesc2 0 13 sampledesc3 0 1thanks for any help,NB |
|
|
RickD
Slow But Sure Yak Herding Master
3608 Posts |
Posted - 2009-04-16 : 09:34:49
|
That doesn't make any sense as you don't have ysnaward in table2.I guess what you are after is:select b.cntTable2 b.strTable2 b.ysnapproved a.ysnawardfrom Table1 ainner join table2 bon a.cntTable1 = b.cntTable2where a.ysnaward <> 1and b.ysnapproved = 1 |
 |
|
|
nbalraj
Starting Member
21 Posts |
Posted - 2009-04-17 : 13:21:12
|
| Hi,Thanks for your reply, let me try to recreate the scenario:table1cnttable1 strdesc ysnApproved1 desc1 12 desc2 13 desc3 04 desc4 15 desc5 16 desc6 1table2cnttable2 cnttable1 ysnAward1 NULL 12 NULL 03 NULL 04 1 05 2 1cnttable1 is the primary key for table1expected results from table1cnttable1 strdesc ysnApproved2 desc2 14 desc4 15 desc5 16 desc6 1the criteria need to use is ysnapproved = 1 and ysnAward = 1all the records from table1, where ysnapproved = 1 and if the record is in table2 show only if the ysnaward = 1hope this helps. and thanks for ur help.-NB |
 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2009-04-17 : 13:57:54
|
| maybe this...declare @t1 table (cnttable1 int,strdesc varchar(10),ysnApproved int)insert @t1select 1, 'desc1', 1 union allselect 2, 'desc2', 1 union allselect 3, 'desc3', 0 union allselect 4, 'desc4', 1 union allselect 5, 'desc5', 1 union allselect 6, 'desc6', 1declare @t2 table (cnttable2 int, cnttable1 int, ysnAward int)insert @t2select 1, NULL, 1 union allselect 2, NULL, 0 union allselect 3, NULL, 0 union allselect 4, 1, 0 union allselect 5, 2, 1select t1.cnttable1 , t1.strdesc , t1.ysnApproved,t2.ysnAwardfrom @t1 t1 left join@t2 t2on t1.cnttable1 = t2.cnttable1where t1.ysnApproved = 1 and ISNULL(t2.ysnAward,1) = 1Result------cnttable1 strdesc ysnApproved ysnAward----------- ---------- ----------- -----------2 desc2 1 14 desc4 1 NULL5 desc5 1 NULL6 desc6 1 NULL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-04-17 : 14:00:04
|
| [code]SELECT t1.*FROM table1 t1LEFT JOIN table2 t2ON t2.cnttable1=t1.cnttable1WHERE t1.ysnApproved=1AND (t2.ysnAward=1 OR t2.ysnAward IS NULL)[/code] |
 |
|
|
nbalraj
Starting Member
21 Posts |
Posted - 2009-04-17 : 22:31:35
|
| thanks guys. |
 |
|
|
|
|
|
|
|