Solved

SQL - Join to columns as one

Posted on 2011-09-16
7
377 Views
Last Modified: 2012-05-12
My current SQL statement is this:
SELECT DISTINCT home_nation, company_nation
FROM jos_users

which returns to me two columns with a listing of countries:

What I want to do is only have my SQL statement return one column of unique columns. So for example:

current return:

home_nation        company_nation
Australia
Canada
                              Canada
                              Bermuda
United Kingdom
                              Australia




Return I want:

unique_nation
Australia
Canada
Canada
Bermuda
United Kingdom
Australia


Thanks for any help!

0
Comment
Question by:elliottbenzle
[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
7 Comments
 
LVL 5

Expert Comment

by:magento
ID: 36549842

Try this.

    SELECT home_nation + ' ' + company_nation AS unique_nation
    FROM jos_user

Open in new window

0
 
LVL 21

Expert Comment

by:JestersGrind
ID: 36549890
If there is always one entry in one of the two columns, you could do something like this.

SELECT CASE WHEN home_nation = '' THEN company_nation ELSE home_nation END AS unique_nation
FROM jos_user

Greg

0
 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36549962
try:

select distinct country from (
  SELECT DISTINCT home_nation Country FROM jos_users
  UNION
  SELECT DISTINCT company_nation FROM jos_users
)
0
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!

 
LVL 51

Expert Comment

by:Huseyin KAHRAMAN
ID: 36549978
What I want to do is only have my SQL statement return one column of unique columns

and

Return I want:

unique_nation
Australia
Canada
Canada
Bermuda
United Kingdom
Australia

contradicts... you should say


Return I want:

unique_nation
Australia
Canada
Bermuda
United Kingdom
Australia

which I posted above...
0
 
LVL 5

Accepted Solution

by:
magento earned 500 total points
ID: 36549983
Tested the below solution.

select concat(home_nation,company_nation) uniq from jos_users
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36550046
or, if its really "NULL" in either column, you could use COALESCE(home_nation, company_nation) ...
0
 
LVL 4

Author Closing Comment

by:elliottbenzle
ID: 36550209
Thanks, that did the trick.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Pivoting oracle table 9 74
SQL Rewrite without the NULLIF 4 25
SQL Pivot table 2 44
SQL Query incorrect syntax near the keyword ON any help would be appreciated 18 30
Creating and Managing Databases with phpMyAdmin in cPanel.
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

749 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