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 2012 Forums
 Transact-SQL (2012)
 Select Help

Author  Topic 

AAAV
Posting Yak Master

152 Posts

Posted - 2014-10-15 : 12:56:38
I have two tables
TableA- ColA1 , ColA2, ColA3
TableB- ColB1,ColB2,ColB3

I want to select the rows from TableB.colB3
where TableB ColB1 is null or ColA1 , TableB colB2 is null or ColA2
for every Row in TableA.ColA3<>null

Can it be done?
Thanks

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2014-10-15 : 13:28:29
It can be done, but from your description it is not clear what the logic should be. It would help if you post some sample data and expected results. For example, in the following scenario, what should be the output?
CREATE TABLE #tableA( cola1 INT, cola2 INT, cola3 INT);
CREATE TABLE #tableB( colb1 INT, colb2 INT, colb3 INT);

INSERT INTO #tableA
( cola1, cola2, cola3 )
VALUES (1,2,0);

INSERT INTO #tableB
( colb1, colb2, colb3 )
VALUES (1, 3, 6), (2,2,7), (1,2,8),(NULL,NULL,9),(NULL,2,10);
If you post sample data in consumable format like this (i.e., something that one can copy to a SSMS window and run), that would make it easier for someone to respond.
Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2014-10-15 : 13:44:07
Here you go

create table TableA
(ColA1 int null,
ColA2 int null,
ColA3 int null)

create table TableB
(ColB1 int null,
ColB2 int null,
ColB3 int null)

insert into TableA values (1,1,4)
insert into TableA values (2,1,null)
insert into TableA values (2,2,6)

Insert into TableB values (1,null,6)
insert into TableB values (1,1,7)
insert into TableB values (2, 1,9)
insert into TableB values (3,1,8)
insert into TableB values (2, 2,8)

select * from TableA where ColA3 is not null
select * from TableB


ColB1 ColB2 ColB3
1 NULL 6 -yes
1 1 7 -yes
2 NULL NULL -yes
3 1 8 -No
2 2 8 -Yes
2 1 9 -No

The table is joined on ColA1=ColB1 but I need the rows from b where ColB2 is null or the corresponding ColA2
Go to Top of Page

AAAV
Posting Yak Master

152 Posts

Posted - 2014-10-16 : 13:31:17
Igot it... thanks
Go to Top of Page
   

- Advertisement -