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


Demonstrates some powerful derived table & ANSI join features. The script shows how to generate combinations of values from 2 tables & match them against a third table.

Click here to copy the following block
create table #a ( a int );
create table #b ( b int );
-- This table contains all combinations of the values from #a & #b
create table #ab ( a int , b int );
go
insert #a values( 1 );
insert #a values( 2 );
select * from #a;
/*
a     
-----------
     1
     2
*/

insert #b values( 5 );
insert #b values( 6 );
select * from #b;
/*
b     
-----------
     5
     6
*/

insert #ab values( 1 , 5 );
insert #ab values( 2 , 6 );
select * from #ab;
/*
a      b     
----------- -----------
     1      5
     2      6
*/

go
-- Objective: Get all combinations of rows from #a & #b irrespective of
--      whether there is one existing in the combinations table.
-- Solution : This one uses one of the least known features of the ANSI joins
--      i.e., using parenthesis to nest joins & build virtual results.
select #a.a , #b.b , #ab.a AS "ab-a" , #ab.b as "ab-b"
from #ab
full join ( #a cross join #b )
on #ab.a = #a.a And #ab.b = #b.b
-- Expected Output:
/*
a      b      ab-a    ab-b    
----------- ----------- ----------- -----------
     1      5      1      5
     1      6 NULL    NULL
     2      5 NULL    NULL
     2      6      2      6
*/

go
drop table #a;
drop table #b;
drop table #ab;
go


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.