Solved

Unions for Dummies

Posted on 2011-03-10
1
415 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
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
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.
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
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

828 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