[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Help creating new column in SQL query

Posted on 2011-09-23
1
Medium Priority
?
368 Views
Last Modified: 2012-05-12
I am creating a SELECT query

I have a table with a lot of columns, I need to take two of these columns and merge them into a new column within the query. These two columns are 'home_city' and 'company_city' I want to merge these two columns into a new column called 'city'. The merging statement looks like this:

SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users

This is what the new column cities will look like.

And I want to insert that into the below Query:

SELECT u.name AS name, u.username AS username, u.person_id AS person_id, u.userid AS userid, u.email AS email, u.title AS title, u.company_name AS company_name, u.company_addr1 AS company_addr1, u.company_addr2 AS company_addr2, u.company_city AS company_city, u.company_state AS company_state, u.company_zip AS company_zip, u.company_nation AS company_nation, u.work_phone1 AS work_phone1, u.phone_ext AS phone_ext, u.fax_number AS fax_number, u.hide_home_addr AS hide_home_addr, u.home_addr1 AS home_addr1, u.home_addr2 AS home_addr2, u.home_city AS home_city, u.home_state AS home_state, u.home_zip AS home_zip, u. home_nation AS home_nation, u.home_phone AS home_phone, u.hide_display AS hide_display, g.name AS usertype FROM #__users AS u INNER JOIN #__core_acl_aro AS aro ON aro.value = u.id INNER JOIN #__core_acl_groups_aro_map AS gm ON gm.aro_id = aro.id INNER JOIN #__core_acl_aro_groups AS g ON g.id = gm.group_id WHERE (u.person_id LIKE '%a%' AND u.userid LIKE '%a%' AND u.home_nation LIKE '%USA%' AND u.home_nation LIKE '%USA%' AND (u.home_city LIKE '%%' OR u.company_city LIKE '%%') AND (u.home_state LIKE '%OH%' OR u.company_state LIKE '%OH%')) AND g.name = 'Registered' ORDER BY name ASC, u.id ASC LIMIT 0, 50

The above query currently returns 47 columns. I want the new query to return 48 columns. The new column will be called 'city' and will contain the value of 'SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users'

My initial attempt is below, but it gives an error: #1242 - Subquery returns more than 1 row

My initial attempt (addition is on second line):
SELECT
 (SELECT DISTINCT home_city city FROM jos_users UNION SELECT DISTINCT company_city FROM jos_users) AS city,
u.name AS name, u.username AS username, u.person_id AS person_id, u.userid AS userid, u.email AS email, u.title AS title, u.company_name AS company_name, u.company_addr1 AS company_addr1, u.company_addr2 AS company_addr2, u.company_city AS company_city, u.company_state AS company_state, u.company_zip AS company_zip, u.company_nation AS company_nation, u.work_phone1 AS work_phone1, u.phone_ext AS phone_ext, u.fax_number AS fax_number, u.hide_home_addr AS hide_home_addr, u.home_addr1 AS home_addr1, u.home_addr2 AS home_addr2, u.home_city AS home_city, u.home_state AS home_state, u.home_zip AS home_zip, u. home_nation AS home_nation, u.home_phone AS home_phone, u.hide_display AS hide_display, g.name AS usertype FROM jos_users AS u INNER JOIN jos_core_acl_aro AS aro ON aro.value = u.id INNER JOIN jos_core_acl_groups_aro_map AS gm ON gm.aro_id = aro.id INNER JOIN jos_core_acl_aro_groups AS g ON g.id = gm.group_id WHERE (u.person_id LIKE '%a%' AND u.userid LIKE '%a%' AND u.home_nation LIKE '%USA%' AND u.home_nation LIKE '%USA%' AND (u.home_city LIKE '%%' OR u.company_city LIKE '%%') AND (u.home_state LIKE '%OH%' OR u.company_state LIKE '%OH%')) AND g.name = 'Registered' ORDER BY name ASC, u.id ASC LIMIT 0, 50

Any help would be appreciated.

Thanks
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
1 Comment
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 2000 total points
ID: 36589183
Hi.

If I understand you correctly and you want an additional column; however, it is not clear what you actually want in that column as you are already displaying home_city and company_city as columns in the original 47. Your UNION will return a list of the distinct cities between both the home_city and company_city field. Therefore, that is why the error states there is more than one row in the result.

What is the intent of this new column?

Could you be trying to set City to the NON-NULL value?

e.g.
COALESCE(u.home_city, u.company_city) AS city

Let me know.

Kevin
0

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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