Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Join in two tables Expand / Collapse
Author
Message
Posted Monday, October 14, 2013 10:00 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 16, 2013 4:00 AM
Points: 2, Visits: 3
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
name, age, money2


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

name, age, money1, age, money2
in one row?
Post #1504479
Posted Monday, October 14, 2013 3:29 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:18 AM
Points: 6,829, Visits: 13,290
You might want to look into PIVOT (or CrossTab, as referenced in the related link in my signature).



Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1504561
Posted Tuesday, October 15, 2013 4:13 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 16, 2013 4:00 AM
Points: 2, Visits: 3
The problem with cross tab or pivot method is that we have to hard-code total number of columns.
Here in the table t2 there may be any number of rows for a given name.
How do I handle that?
Post #1504665
Posted Tuesday, October 15, 2013 8:45 AM
SSChasing Mays

SSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing MaysSSChasing Mays

Group: General Forum Members
Last Login: Wednesday, September 24, 2014 8:15 AM
Points: 628, Visits: 2,129
You can still cross tab or pivot, but you will need to use dynamic SQL.
Post #1504796
Posted Tuesday, October 15, 2013 9:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 9:18 AM
Points: 6,829, Visits: 13,290
ankit.gupta1 (10/15/2013)
The problem with cross tab or pivot method is that we have to hard-code total number of columns.
Here in the table t2 there may be any number of rows for a given name.
How do I handle that?


Even though I prefer to do stuff like that in the application layer, I'd use the DynamicCrossTab approach as referenced in my signature, if needed.




Lutz
A pessimist is an optimist with experience.

How to get fast answers to your question
How to post performance related questions
Links for Tally Table , Cross Tabs and Dynamic Cross Tabs , Delimited Split Function
Post #1504833
Posted Wednesday, November 27, 2013 5:35 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 9:07 PM
Points: 3,420, Visits: 5,347
ankit.gupta1 (10/14/2013)
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
name, age, money2


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

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


I have two questions:
1. Why do you need to display 2 columns with age when they come from 1 row of T1? Wouldn't displaying it once be enough?
2. Are you updating age daily in your table?



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1518235
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse