Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Search for combination of column values in different rows.

Total Hit ( 1578)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


One solution for searching for combination of column values in different rows. Please see the script for more details & example.

Click here to copy the following block
create table #Tbl1
(
number int primary key,
name varchar (25)
);

create table #Tbl2
(
number int,
field1 varchar (5),
field2 varchar (5)
);
go
insert into #Tbl1 values ( 1 , 'Value1' );
insert into #Tbl1 values ( 2 , 'Value2' );
go
insert into #Tbl2 values ( 1 , 'from', 'me');
insert into #Tbl2 values ( 1 , 'from', 'me1');
insert into #Tbl2 values ( 1 , 'from1', 'me');
insert into #Tbl2 values ( 1 , 'to', 'you');
insert into #Tbl2 values ( 1 , 'to', 'me');
go
insert into #Tbl2 values ( 2 , 'to', 'you');
insert into #Tbl2 values ( 2 , 'to', 'you');
insert into #Tbl2 values ( 2 , 'to1', 'you');
insert into #Tbl2 values ( 2 , 'to', 'you1');
go
select* from #Tbl1;
/*
number   name           
----------- -------------------------
     1 Value1
     2 Value2
*/

select* from #Tbl2;
/*
number   field1 field2
----------- ------ ------
     1 from  me
     1 from  me1
     1 from1 me
     1 to   you
     1 to   me
     2 to   you
     2 to   you
     2 to1  you
     2 to   you1
*/

go
select g1.Number , g1.name
from #Tbl1 as g1
join #Tbl2 as g2
 on (g1.number = g2.number)
where ( g2.field1 = 'from' And g2.field2 = 'me' ) Or
    ( g2.field1 = 'to' And g2.field2 = 'you' )
group by g1.Number , g1.name
having sum( case
        when ( g2.field1 = 'from' And g2.field2 = 'me' ) then -1
        when ( g2.field1 = 'to' And g2.field2 = 'you' ) then 1
    end ) = 0;
go
drop table #Tbl1;
drop table #Tbl2;


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.