Solved

SQL oracle - Union query issue

Posted on 2013-01-10
6
566 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
6 Comments
 
LVL 73

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 76

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 76

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 31

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Cannot open form error 6 48
monitor queries that use too much tempdb log 20 35
SQL Query Syntax Error 9 33
dates - loop 12 41
I remember the day when someone asked me to create a user for an application developement. The user should be able to create views and materialized views and, so, I used the following syntax: (CODE) This way, I guessed, I would ensure that use…
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…
This video explains at a high level about the four available data types in Oracle and how dates can be manipulated by the user to get data into and out of the database.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now