Solved

Left join of the same table but take 2 different columns .

Posted on 2014-01-24
5
199 Views
Last Modified: 2014-01-31
i have a table A , with columns A.A,A.B,A.C,A.D . For each matching row in the table my query should give 2 rows one row with A.A as original and one row with A.B as original , how can i accomplish this .
Resultset :
1. A.A as original,A.C,A.D
2. A.B as original,A.C,A.D
0
Comment
Question by:FranklinRaj22
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 66

Expert Comment

by:Jim Horn
ID: 39806940
I think we need to see some mockup data of what you're trying to pull off here, as the term 'original' isn't real intuitive.
0
 

Author Comment

by:FranklinRaj22
ID: 39806962
Table A

ColA           ColB   ColC     ColD

America     USA     1          2
Russia        USSR    5         6

Result should be
Col1        Col2   Col3
America   1        2
USA          1        2
Russia      5       6
USSR         5       6

I am able to accomplish this with but want to know if there are any better ways to do it ...
(select ColA as col1,ColC as col2,ColD as col3 from A) UNION (select ColB as col1,ColC as col2,ColD as col3 from A)
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39807207
that is not a "left join", but a UNION ALL of the same table ..
select a.cola, a.colc, a.cold from tableA a
UNION ALL
select a.colb, a.colc, a.cold from tableA a

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39807214
note: UNION should be avoided whenever you can, and use UNION ALL instead.
UNION is performance an implicit DISTINCT over the resulting outcome, which usually is not required, and hence just wasting resources.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 39807599
No need to scan the table twice; instead, use a CROSS JOIN.  You could also use a CROSS APPLY.:

SELECT
    CASE WHEN whichCol = 'A' THEN A.ColA ELSE A.ColB END AS Col1,
    A.ColC,
    A.ColD
FROM dbo.tablename A
CROSS JOIN (
    SELECT 'A' AS whichCol UNION ALL
    SELECT 'B'
) AS whichCols
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question