Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-24
5
Medium Priority
?
200 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 70

Accepted Solution

by:
Scott Pletcher earned 1500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

722 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