Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Combine two Oracel queries

Posted on 2011-03-22
6
Medium Priority
?
373 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
[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
  • 4
6 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
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 video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
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.

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