结合两个Oracle查询

时间:2014-05-29 15:18:50

标签: sql oracle

我是oracle / sql的初学者。我可以写基本的查询,但现在我开始了。我有两个想要结合的问题。我正在寻找的输出是这样的(在一行中):

批次#
状态
分钟
页数

以下是两个单独的查询:

   SELECT aq.batchnum, aq.status, (dateended - datestarted) * 24 * 60 minutes
    FROM archivedqueue aq INNER JOIN scanninglog sl ON aq.batchnum = sl.batchnum
   WHERE     aq.datestarted BETWEEN TO_DATE ('27-MAY-2014 00:00:00', 'dd-mon-yyyy hh24:mi:ss')
                                AND TO_DATE ('29-MAY-2014 23:59:59', 'dd-mon-yyyy hh24:mi:ss')
         AND sl.usernum = '3724'
         AND sl.actionnum = '200'
ORDER BY aq.batchnum DESC;


SELECT COUNT (*)
  FROM archivedqueue aq
       INNER JOIN itemdata id ON aq.batchnum = id.batchnum
       INNER JOIN itemdatapage idp ON idp.itemnum = id.itemnum
       INNER JOIN scanninglog sl ON aq.batchnum = sl.batchnum
 WHERE aq.batchnum = '648353' AND sl.actionnum = '202';

我试图做这样的事情,但我不确定如何将q1中的结果与q2中的查询联系起来

    SELECT q1.batchnum,
       q1.status,
       q1.minutes,
       q2.page_count
  FROM (  SELECT aq.batchnum, aq.status, (dateended - datestarted) * 24 * 60 minutes
            FROM archivedqueue aq INNER JOIN scanninglog sl ON aq.batchnum = sl.batchnum
           WHERE     aq.datestarted BETWEEN TO_DATE ('27-MAY-2014 00:00:00',
                                                     'dd-mon-yyyy hh24:mi:ss')
                                        AND TO_DATE ('29-MAY-2014 23:59:59',
                                                     'dd-mon-yyyy hh24:mi:ss')
                 AND sl.usernum = '3724'
                 AND sl.actionnum = '200'
        ORDER BY aq.batchnum DESC) q1,
       (SELECT COUNT (*) page_count
          FROM archivedqueue aq
               INNER JOIN itemdata id ON aq.batchnum = id.batchnum
               INNER JOIN itemdatapage idp ON idp.itemnum = id.itemnum
               INNER JOIN scanninglog sl ON aq.batchnum = sl.batchnum
         WHERE sl.actionnum = '202' AND aq.batchnum = q1.batchnum) q2

2 个答案:

答案 0 :(得分:1)

给你非ANSI SQL版本,因为你在Oracle DB中运行(我更喜欢:) :):

  SELECT aq.batchnum,
         aq.status,
         (dateended - datestarted) * 24 * 60 minutes,
         COUNT (*) page_count
    FROM archivedqueue aq,
         itemdata id,
         itemdatapage idp,
         (SELECT *
            FROM scanninglog
           WHERE usernum = '3724' AND actionnum IN ('200', '202')) sl
   WHERE     aq.batchnum = id.batchnum
         AND idp.itemnum = id.itemnum
         AND aq.batchnum = sl.batchnum
         AND aq.batchnum = '648353'
         AND aq.datestarted BETWEEN TO_DATE ('27-MAY-2014 00:00:00', 'dd-mon-yyyy hh24:mi:ss')
                                AND TO_DATE ('29-MAY-2014 23:59:59', 'dd-mon-yyyy hh24:mi:ss')
GROUP BY aq.batchnum, aq.status, (dateended - datestarted) * 24 * 60
ORDER BY aq.batchnum DESC;

答案 1 :(得分:0)

我认为您想要使用GROUP BY功能。如果不知道你的表格结构等,很难确定,但它可能看起来像这样:

SELECT aq.batchnum, aq.status, (dateended - datestarted)*24*60 Minutes, count(*)
FROM archivedqueue aq
     INNER JOIN scanninglog sl
     ON aq.batchnum = sl.batchnum
     INNER JOIN itemdata id
     ON aq.batchnum = id.batchnum
     INNER JOIN itemdatapage idp
     ON idp.itemnum = id.itemnum
WHERE (
      aq.datestarted 
      BETWEEN TO_DATE( '27-MAY-2014 00:00:00', 'dd-mon-yyyy hh24:mi:ss' ) 
      AND TO_DATE( '29-MAY-2014 23:59:59', 'dd-mon-yyyy hh24:mi:ss' )
      )
AND sl.usernum = '3724'
AND sl.actionnum = '200'
GROUP BY aq.batchnum, aq.status, (dateended - datestarted)*24*60
ORDER BY aq.batchnum DESC