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)
 SQL

Author  Topic 

niranjan1988
Starting Member

5 Posts

Posted - 2013-05-08 : 14:22:26
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

1974 Posts

Posted - 2013-05-08 : 14:28:45
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 - 2013-05-08 : 14:33:02
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

1974 Posts

Posted - 2013-05-08 : 14:35:58
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 - 2013-05-08 : 14:40:30
output will be

Null helo
ranjan Null

But i need

ranjan helo
Go to Top of Page

MIK_2008
Master Smack Fu Yak Hacker

1054 Posts

Posted - 2013-05-08 : 14:53:12
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 - 2013-05-08 : 14:57:38
how u r applying the max function to varchar column?
Go to Top of Page

chadmat
The Chadinator

1974 Posts

Posted - 2013-05-08 : 15:10:13
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

1974 Posts

Posted - 2013-05-08 : 15:11:57
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 - 2013-05-08 : 15:53:39
it worked.. thanks
Go to Top of Page

UnemployedInOz
Yak Posting Veteran

54 Posts

Posted - 2013-05-09 : 02:00:46
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
   

- Advertisement -