Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 SQL
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

niranjan1988
Starting Member

5 Posts

Posted - 05/08/2013 :  14:22:26  Show Profile  Reply with Quote
I hAVE 2 TABLES...

Table1
-------
c1 c2
NUll helo

Table 2
--------
c1 c2
ranjan null

I want to display as ...

c1 c2
ranjan helo

chadmat
The Chadinator

USA
1974 Posts

Posted - 05/08/2013 :  14:28:45  Show Profile  Visit chadmat's Homepage  Reply with Quote
There is nothing to join these tables on. Given the limited dataset, you could create a Cartesian product, and get what you want, but I don't think that that would be what you want if there is more data in the tables.

-Chad
Go to Top of Page

niranjan1988
Starting Member

5 Posts

Posted - 05/08/2013 :  14:33:02  Show Profile  Reply with Quote
Assume that c3 colum is key column to join both tables...

Table1
-------
c1 c2 c3
NUll helo K1

Table 2
--------
c1 c2 C3
ranjan null K1
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 05/08/2013 :  14:35:58  Show Profile  Visit chadmat's Homepage  Reply with Quote
SELECT t2.c1, t1.c2
FROM Table1 t1 join Table2 t2 on t1.c3=t2.c3

-Chad
Go to Top of Page

niranjan1988
Starting Member

5 Posts

Posted - 05/08/2013 :  14:40:30  Show Profile  Reply with Quote
output will be

Null helo
ranjan Null

But i need

ranjan helo
Go to Top of Page

MIK_2008
Flowing Fount of Yak Knowledge

Pakistan
1054 Posts

Posted - 05/08/2013 :  14:53:12  Show Profile  Reply with Quote
declare @t1 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
declare @t2 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
insert into @t1 values (null,'helo','K1')
insert into @t2 values ('ranjan',NULL,'K1')
SELECT Max(t2.col1), Max(t1.col2)
FROM @t1 t1 join @t2 t2 on t1.col3=t2.col3

Cheers
MIK
Go to Top of Page

niranjan1988
Starting Member

5 Posts

Posted - 05/08/2013 :  14:57:38  Show Profile  Reply with Quote
how u r applying the max function to varchar column?
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 05/08/2013 :  15:10:13  Show Profile  Visit chadmat's Homepage  Reply with Quote
quote:
Originally posted by MIK_2008

declare @t1 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
declare @t2 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
insert into @t1 values (null,'helo','K1')
insert into @t2 values ('ranjan',NULL,'K1')
SELECT Max(t2.col1), Max(t1.col2)
FROM @t1 t1 join @t2 t2 on t1.col3=t2.col3

Cheers
MIK



Why MAX()?

-Chad
Go to Top of Page

chadmat
The Chadinator

USA
1974 Posts

Posted - 05/08/2013 :  15:11:57  Show Profile  Visit chadmat's Homepage  Reply with Quote
quote:
Originally posted by niranjan1988

output will be

Null helo
ranjan Null

But i need

ranjan helo




It shouldn't be if you have described your data properly.

declare @t1 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
declare @t2 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
insert into @t1 values (null,'helo','K1')
insert into @t2 values ('ranjan',NULL,'K1')

SELECT t2.col1, t1.col2
FROM @t1 t1 join @t2 t2 on t1.col3=t2.col3


-Chad
Go to Top of Page

niranjan1988
Starting Member

5 Posts

Posted - 05/08/2013 :  15:53:39  Show Profile  Reply with Quote
it worked.. thanks
Go to Top of Page

UnemployedInOz
Yak Posting Veteran

Australia
54 Posts

Posted - 05/09/2013 :  02:00:46  Show Profile  Reply with Quote
declare @t1 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
declare @t2 table (col1 varchar(10),col2 varchar(10),col3 varchar(10))
insert into @t1 values (null,'helo','K1')
insert into @t1 values (null,'helo2','K2')
insert into @t2 values ('ranjan',NULL,'K1')
insert into @t2 values ('ranjan2',NULL,'K2')

SELECT max(t2.col1), max(t1.col2)
FROM @t1 t1 join @t2 t2 on t1.col3=t2.col3
group by t1.col3,t2.col3
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000