[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL oracle - Union query issue

Posted on 2013-01-10
6
Medium Priority
?
576 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

650 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