Solved

Combine two Oracel queries

Posted on 2011-03-22
6
367 Views
Last Modified: 2012-06-27
Hi,

I just need a bit of help with the SQL.  I have two queries, and would like to combine them into once query as efficiently as possible.  The tables that I am working with are large (millions of rows) and the server is quite loaded.  Any suggestions on how to improve the efficiency of these queries is appreciated, I don't know SQL very well.

Here are two queries, they are identical except that each one returns the totals if COLUMN_D = Option_1 vs Option_2.

What I'd like is to just have one query, and have the totals for Option_1 and Option_2 (both from column_D) split into two columns.

Thank you

SELECT  trunc(CREATION_DATE) CREATED_DATE,

    CASE 
        WHEN COLUMN_A = 'xxxxxxx'
            THEN 'x'
        WHEN COLUMN_A = 'yyyyyyy'
            THEN 'y'
        ELSE 'OTHER'
    END COLUMN_A,
    
COUNT (COLUMN_D) TOTAL_1         
FROM DB.TABLE_A 

Where trunc(SYSDATE) - 1 <trunc(CREATION_DATE)
and COLUMN_D = 'OPTION_2'

group by trunc(CREATION_DATE), COLUMN_A;

Open in new window

SELECT  trunc(CREATION_DATE) CREATED_DATE,

    CASE 
        WHEN COLUMN_A = 'xxxxxxx'
            THEN 'x'
        WHEN COLUMN_A = 'yyyyyyy'
            THEN 'y'
        ELSE 'OTHER'
    END COLUMN_A,
    
COUNT (COLUMN_D) TOTAL_1         
FROM DB.TABLE_A 

Where trunc(SYSDATE) - 1 <trunc(CREATION_DATE)
and COLUMN_D = 'OPTION_1'

group by trunc(CREATION_DATE), COLUMN_A;

Open in new window

0
Comment
Question by:TelMaco
  • 4
6 Comments
 
LVL 73

Accepted Solution

by:
sdstuber earned 250 total points
ID: 35191562
SELECT   TRUNC(creation_date) created_date,
         CASE WHEN column_a = 'xxxxxxx' THEN 'x' WHEN column_a = 'yyyyyyy' THEN 'y' ELSE 'OTHER' END
             column_a,
         COUNT(CASE WHEN column_d = 'OPTION_1' THEN 1 END) total_1,
         COUNT(CASE WHEN column_d = 'OPTION_2' THEN 1 END) total_2
    FROM db.table_a
   WHERE creation_date > TRUNC(SYSDATE)  AND column_d IN ('OPTION_1', 'OPTION_2')
GROUP BY TRUNC(creation_date), column_a
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35191568
Note I removed the TRUNC around creation_date in your where clause.

It's not needed and actually hurts performance if you have that column indexed
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35191594
oops,  missed midnight though

creation_date > TRUNC(SYSDATE)

should be


creation_date >= TRUNC(SYSDATE)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 32

Expert Comment

by:awking00
ID: 35191699
Is there only option_1 and option_2 or are there others?
0
 
LVL 73

Expert Comment

by:sdstuber
ID: 35191722
if column_d is restricted to only those 2 strings then the where clause IN condition on them isn't needed
but it shouldn't hurt to have it either
0
 

Author Comment

by:TelMaco
ID: 35191805
There are other options for COLUMN_D, so the IN statement is helpful.  This works perfectly.  Thank you!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

863 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

25 Experts available now in Live!

Get 1:1 Help Now