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

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Join in two tables
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yaman909
Starting Member

2 Posts

Posted - 10/14/2013 :  11:56:05  Show Profile  Reply with Quote
I have two tables:
T1 and T2.
T1 has 1 row as
name and age

T2 has 2 rows as
name, money1
name, money2

I use left outer join and get two rows in the resulting table.
As name, age, money1
and name, age, money2

Is there any way using which i can get data like:

name, age, money1, age, money2
in one row?

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/14/2013 :  12:55:01  Show Profile  Reply with Quote
If you always have only maximum of two money

SELECT t1.name,
t1.age,
MAX(CASE WHEN Seq=1 THEN t2.money END) AS money1,
t1.age,
MAX(CASE WHEN Seq=2 THEN t2.money END) AS money2
FROM T1 t1
INNER JOIN (SELECT name,money,ROW_NUMBER() OVER (PARTITION BY name ORDER BY name) AS Seq
            FROM T2
            GROUP BY name
           )t2
ON t2.name = t1.name
GROUP BY t1.name,t1.age


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

yaman909
Starting Member

2 Posts

Posted - 10/15/2013 :  06:12:15  Show Profile  Reply with Quote
visakh16,

The rows in the second table can be any number for a given name.
i.e money value can go up to any value for a particular name.
How do I handle that?
Go to Top of Page

divya.ce
Starting Member

India
16 Posts

Posted - 10/15/2013 :  08:20:49  Show Profile  Reply with Quote
Please find one solution below, however in this solution at least you should be aware max how many money values will be corresponding to one name..

declare @t1 table(name varchar(100),age varchar(10))
declare @t2 table(name varchar(100),mny int)
insert into @t1
values('a','20')
insert into @t2
values('a',100),
('a',200),
('a',300)


select *
from @t1 t1
left join @t2 t2
on t1.name = t2.name

select *
from
(
select t1.name,t1.age, t2.mny,
newcolname = cast('Money ' as varchar) + cast(ROW_NUMBER() OVER( PARTITION BY t1.name order by mny) as varchar)
from @t1 t1
left join @t2 t2
on t1.name = t2.name
)t
pivot
(min(mny) for newcolname in ([Money 1],[Money 2],[Money 3]))P
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 10/16/2013 :  02:13:07  Show Profile  Reply with Quote
quote:
Originally posted by yaman909

visakh16,

The rows in the second table can be any number for a given name.
i.e money value can go up to any value for a particular name.
How do I handle that?



So are you looking at dynamically pivotting money values?

If yes see

http://beyondrelational.com/modules/2/blogs/70/posts/10840/dynamic-pivot-in-sql-server-2005.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New 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.08 seconds. Powered By: Snitz Forums 2000