?
Solved

Combine two Oracel queries

Posted on 2011-03-22
6
Medium Priority
?
375 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 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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 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.
Suggested Courses

862 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