Solved

SQL oracle - Union query issue

Posted on 2013-01-10
6
574 Views
Last Modified: 2013-01-12
Hello all

I have this query bellow that work great but i would like to know if i can make it better that that?

Is there a way to short it up so that it do the same thing?

Thanks again

  SELECT   BILL,
           CARRIER_ID,
           MILES,
           CUBE,
           BEST_DEP,
           FREIGHT_COST
    FROM   LOAD
   WHERE   BEST_DEP BETWEEN TO_DATE ('2013-01-01', 'YYYY-MM-DD')
                        AND  TO_DATE ('2013-12-31', 'YYYY-MM-DD')
           AND CARRIER_ID IN
                    ('UPS',
                     'PUROLATOR')
UNION
  SELECT   BOL,
           CARRIER_ID,
           MILES,
           CUBE,
           BEST_DEP,
           FREIGHT_COST
    FROM   FREIGHT_MOVEMENT_HIST
   WHERE   BEST_DEP BETWEEN TO_DATE ('2012-01-01', 'YYYY-MM-DD')
                        AND  TO_DATE ('2012-12-31', 'YYYY-MM-DD')
           AND CARRIER_ID IN
                    ('UPS',
                     'PUROLATOR')
ORDER BY   CARRIER_ID;

Open in new window

0
Comment
Question by:Wilder1626
[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
6 Comments
 
LVL 74

Assisted Solution

by:sdstuber
sdstuber earned 167 total points
ID: 38765507
if the data between the sets is distinct you should change  "UNION" to "UNION ALL"

other than that, there isn't much to either query so nothing really to change there.


you could look at indexes on the respective tables to help, what does the current explain plan look like?  - use dbms_xplan.display  -  please NO screen shots
0
 
LVL 77

Expert Comment

by:slightwv (䄆 Netminder)
ID: 38765564
Maybe a Materialized View?
0
 
LVL 11

Author Comment

by:Wilder1626
ID: 38766874
Hello all

I tried the union all but i have the same result.

slightwv,
i'm pretty new to SQL but what is a Materialized View?

How would i build the query?
0
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 166 total points
ID: 38766900
In a nutshell, a materialized view is a select statement whose results are stored in physical form.  It is a good way to take large complex queries and more or less pre-execute them, get the results, and store them.  then anything needing the data can access it without executing the large query.  Just select from the view.

It will take the select statement you have and create a table of the results.  It will be updated either at a set time or as the underlying data changes.  How and when you refresh the view is up to your reauirements.

There is a ton of information out there on them if you look around.

Here's a good starting point from the online docs:
http://docs.oracle.com/cd/E11882_01/server.112/e25789/schemaob.htm#CNCPT88875
0
 
LVL 32

Accepted Solution

by:
awking00 earned 167 total points
ID: 38767297
Just a note - If best_dep contains a time element, you will need to restructure your where clauses to accommodate any records for 12-31. For example,
where best_dep between .... and to_date('2012-12-31 23:59:59','yyyy-mm-dd hh24:mi:ss')
or
where best_dep >= to_date('2012-01-01','yyyy-mm-dd')
    and best_dep <to_date('2013-01-01','yyyy-mm-dd')
0
 
LVL 11

Author Closing Comment

by:Wilder1626
ID: 38769941
Thanks a lot for all this information
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
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 explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
Via a live example, show how to take different types of Oracle backups using RMAN.

707 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