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

x
?
Solved

Combine SQL Queries?

Posted on 2007-11-15
4
Medium Priority
?
679 Views
Last Modified: 2013-12-19
Dear Experts -

I just would like to join these 2 queries into a single one using Scalar Sub Queries, in line views or any efficient method.
The column Activity_id is the Primary key.

Thanks.
Query 1: 
--------
SELECT DISTINCT om.queue_id,
                NVL (SUM (1), 0) received_contacts,
                inq.asa,
                inq.work_time + inq.wrap_time in_time,
                inq.work_time work_time,
                inq.wrap_time wrap_time
           FROM e_history_mgmt om,
                (SELECT   activity_id,
                          NVL (AVG (CASE
                                     WHEN object_operation = 410
                                      THEN event_duration
                                     ELSE 0
                                    END), 0) asa,
                          NVL (SUM (CASE
                                     WHEN reason1 = 0
                                     AND NVL (reason3, 0) = 0
                                     AND object_operation IN (541, 542, 543, 544, 545, 546, 547, 548, 411)
                                      THEN event_duration
                                     ELSE 0
                                    END),
                               0) work_time,
                          NVL (SUM (CASE
                                     WHEN reason1 = 1
                                     AND NVL (reason3, 0) = 0
                                     AND object_operation IN (541, 543, 544, 545, 546, 547, 548, 411)
                                      THEN event_duration
                                     ELSE 0
                                    END),
                               0) wrap_time
                     FROM e_history_mgmt
                    WHERE queue_id > 0
                 GROUP BY activity_id) inq
          WHERE om.queue_id > 0
            --AND om.activity_id = 22781
            AND om.event_date >= 1190970000000   --po_retdateinsecs1
            AND om.event_date < 1195142400000   --po_retdateinsecs2
             AND om.queue_id = 1005
            AND om.application_id = 2000
            AND om.activity_id = inq.activity_id
       GROUP BY om.queue_id,
                inq.asa,
                inq.work_time + inq.wrap_time,
                inq.work_time,
                inq.wrap_time;
 
 
Query 2:
--------
SELECT om.queue_id, count(inq.customer_exit_datetime) --, inq.first_pickup_datetime
  FROM e_history_mgmt om,
       (SELECT   activity_id,
                 NVL (MAX (CASE
                            WHEN object_operation = 410
                             THEN event_date
                            ELSE 0
                           END), 0) first_pickup_datetime,
                 NVL (MAX (CASE
                            WHEN object_operation = 544
                             THEN event_date
                            ELSE 0
                           END), 0) completion_datetime,
                 NVL (MAX (CASE
                            WHEN object_operation = 412
                             THEN event_date
                            ELSE 0
                           END), 0) customer_exit_datetime
            FROM e_history_mgmt
           WHERE queue_id > 0
        GROUP BY activity_id) inq
 WHERE om.queue_id > 0
   -- AND om.activity_id = 22781
   AND om.event_date >= 1190970000000   --po_retdateinsecs1
   AND om.event_date < 1195142400000   --po_retdateinsecs2
   AND om.queue_id = 1005
   AND om.application_id = 2000
   AND om.activity_id = inq.activity_id
   AND INQ.completion_datetime != 0
 AND (  inq.customer_exit_datetime < inq.first_pickup_datetime
    OR inq.first_pickup_datetime = 0) 
group by om.queue_id

Open in new window

0
Comment
Question by:sventhan
[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
  • 2
  • 2
4 Comments
 
LVL 74

Accepted Solution

by:
sdstuber earned 2000 total points
ID: 20291688
when you say "join"  what is it you're trying to get from the two queries?

what does the current output of each query look like and what do you want the combined query output to be?
0
 
LVL 18

Author Comment

by:sventhan
ID: 20292418
Sdstuber -

(please not worth spending time on this query if you're busy...).

But, I always welcome your ideas .....

I've to calculate the abondoned_time (2nd query) into the select list of first query.
The idea here is based on the Queue (dept) how many events are received, handled and abandoned etc..,
The history table stores the customers live chat (electronic response) transcripts with date and time.



i've done something like below...



SELECT   ou.queue_id,
         SUM (ou.received_contacts) rec_contats,
         ROUND (SUM (ou.asa) ) asa,
         SUM (ou.in_time) in_time,
         SUM (ou.work_time) work_time,
         SUM (ou.wrap_time) wrap_time,
         ou.abondoned_datetime abt <--- count(abondoned time greater than 0
    FROM (SELECT   om.queue_id,
                   NVL (COUNT (DISTINCT om.activity_id), 0) received_contacts,
                   inq.asa,
                   inq.work_time + inq.wrap_time in_time,
                   inq.work_time work_time,
                   inq.wrap_time wrap_time,
                   inq.customer_exit_datetime cus_exit_time,
                   inq.completion_datetime,
                   inq.first_pickup_datetime,
                   NVL (CASE
                         WHEN (     (inq.customer_exit_datetime < inq.first_pickup_datetime)
                               AND inq.customer_exit_datetime != 0)
                          THEN customer_exit_datetime
                         ELSE 0
                        END,
                        0) abondoned_datetime <-- i've to include this in the outer query as count
              FROM event_history_mgmt om,
                   (SELECT   activity_id,
                             NVL (MAX (CASE
                                        WHEN object_operation = 410
                                         THEN event_date
                                        ELSE 0
                                       END), 0) first_pickup_datetime,
                             NVL (MAX (CASE
                                        WHEN object_operation = 544
                                         THEN event_date
                                        ELSE 0
                                       END), 0) completion_datetime,
                             NVL (MAX (CASE
                                        WHEN object_operation = 412
                                         THEN event_date
                                        ELSE 0
                                       END), 0) customer_exit_datetime,
                             NVL (AVG (CASE
                                        WHEN object_operation = 410
                                         THEN event_duration
                                        ELSE 0
                                       END), 0) asa,
                             NVL (SUM (CASE
                                        WHEN reason1 = 0
                                        AND NVL (reason3, 0) = 0
                                        AND object_operation IN (541, 542, 543, 544, 545, 546, 547, 548, 411)
                                         THEN event_duration
                                        ELSE 0
                                       END),
                                  0) work_time,
                             NVL (SUM (CASE
                                        WHEN reason1 = 1
                                        AND NVL (reason3, 0) = 0
                                        AND object_operation IN (541, 543, 544, 545, 546, 547, 548, 411)
                                         THEN event_duration
                                        ELSE 0
                                       END),
                                  0) wrap_time
                        FROM event_history_mgmt
                       WHERE queue_id > 0
                    GROUP BY activity_id) inq
             WHERE om.queue_id > 0
              -- AND om.activity_id = 22781
               AND om.event_date >= 1190970000000   --po_retdateinsecs1
               AND om.event_date < 1195142400000   --po_retdateinsecs2
               AND om.queue_id in (1005,1006,1031,1018)
               AND om.application_id = 2000
               AND om.activity_id = inq.activity_id
          GROUP BY om.queue_id,
                   inq.asa,
                   inq.work_time + inq.wrap_time,
                   inq.work_time,
                   inq.wrap_time,
                   inq.customer_exit_datetime,
                   inq.completion_datetime,
                   inq.first_pickup_datetime) ou
GROUP BY ou.queue_id,
         ou.abondoned_datetime

0
 
LVL 74

Expert Comment

by:sdstuber
ID: 20293913
thanks for the points but I really didn't do anything unless my questions were enough to prompt you in the right direction.

I assume you got it to work though, so I'm glad I helped even if I don't quite know how.

:)
0
 
LVL 18

Author Comment

by:sventhan
ID: 20295652
Yes. I got it worked.

You deserve the points, because i've seen you had provided solutions to similar kind of queries in various posts before.  In a way those answers gave me some directions to sovle my own problem.

Thanks for your good work here in EE.

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
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
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

610 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