Solved

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

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

828 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