Solved

SQL - Join to columns as one

Posted on 2011-09-16
7
372 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
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:HainKurt
ID: 36549962
try:

select distinct country from (
  SELECT DISTINCT home_nation Country FROM jos_users
  UNION
  SELECT DISTINCT company_nation FROM jos_users
)
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 51

Expert Comment

by:HainKurt
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 142

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Join & Write a Comment

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now