Solved

Unions for Dummies

Posted on 2011-03-10
1
417 Views
Last Modified: 2012-05-11
I created a query that involved a bunch of joins.  Had a real SQL pro look at the query results and apparently they included too much.  The pro redid the query using a UNION.

Can someone explain to me what the data would look like in general for the pro to use a join?

What are the steps in deciding what select statements on each side of the join would look like?  I guess I'm asking for "how to do a union" explanation.
0
Comment
Question by:lcor
[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 77

Accepted Solution

by:
slightwv (䄆 Netminder) earned 500 total points
ID: 35098594
The simple on a straight UNION.

Take the unique results from: Give me everything from queryA add it to the results of give me everything from queryB.

A "UNION ALL" doesn't take unique records.

The caveats:
The number of columns must match and the datatypes for each individual column across the multiple queries must match.

You can do:
select 'string1' col1 , to_number(1) col2 from dual
union
select 'string2' col1 , to_number(2) col2 from dual

You cannot do:

select 'string1' col1 , to_number(1) col2 from dual
union
select to_number(2) col1 , 'string2' col2 from dual

The data types don't match.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

617 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