Solved

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

Posted on 2014-01-24
5
198 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 65

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how the fundamental information of how to create a table.

752 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