Solved

Combine two Oracel queries

Posted on 2011-03-22
6
369 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 74

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 74

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 74

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 74

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

Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Via a live example, show how to take different types of Oracle backups using RMAN.
This video explains what a user managed backup is and shows how to take one, providing a couple of simple example scripts.

856 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