?
Solved

SQL oracle - Union query issue

Posted on 2013-01-10
6
Medium Priority
?
575 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 668 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
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!

 
LVL 77

Assisted Solution

by:slightwv (䄆 Netminder)
slightwv (䄆 Netminder) earned 664 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 668 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
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 Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.
This video shows how to recover a database from a user managed backup
Suggested Courses

770 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