sql server - require to form a sql query -


i working on preparing query stuck.

consider tables below:

table1

id key col1 -- --- -----  1  1   abc  2  2   d  3  3   s  4  4   xyz 

table2

id col1 foreignkey -- ---- ---------- 1   12    1 2   13    1 3   14    1 4   12    2 5   13    2 

now need select records table1 corresponding entries in table2 not have col1 value 12. challenge after applying join though skip value 1 corresponding col1 equal 12 still has multiple rows values 13, 14 have same foreignkey. want if there single row having value 12 should not pick id @ table1.

how can form query this?

the output need above table structure want records table1 col1 value table2 not have value 14. query should return me row 2 table1 , not row 1.

another way of doing that. first 2 queries making sample data.

;with t1(id ,[key] ,col1) (  select 1 , 1 ,  'abc' union  select 2 , 2  , 'd' union  select 3 , 3  , 's' union  select 4 , 4  , 'xyz'  )  ,t2(id ,col1, foreignkey) ( select 1  , 12   , 1 union select 2  , 13   , 1 union select 3  , 14   , 1 union select 4   ,12   , 2 union select 5   ,13  ,  2 ) select id, [key], col1 t1 id not in (select t2.id                  t2                  inner join t1 on t1.id = t2.foreignkey                  t2.col1 = 14) 

Comments