Solved

Help creating new column in SQL query

Posted on 2011-09-23
1
366 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 500 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

Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
Steps to create a PostgreSQL RDS instance in the Amazon cloud. We will cover some of the default settings and show how to connect to the instance once it is up and running.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

695 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