如何在内部查询中传递外部查询的表

时间:2012-04-18 04:48:52

标签: sql oracle select

这是我的整个查询

    SELECT   empmst.emp_id, empmst.emp_name,
         (SELECT RTRIM
                    (XMLAGG (XMLELEMENT (e, d || ',')).EXTRACT ('//text()').EXTRACT
                                                                   ('//text()'),
                     ','
                    )
            FROM (SELECT TO_DATE ('01-04-2012', 'dd-MM-yyyy') - 1
                         + ROWNUM AS d
                    FROM all_objects
                   WHERE TO_DATE ('01-04-2012', 'dd-MM-yyyy') - 1 + ROWNUM <=
                                          TO_DATE ('30-04-2012', 'dd-MM-yyyy')
                  MINUS
                  SELECT tsd.ts_date
                    FROM ts_dtl tsd
                   WHERE empmst.emp_id = tsd.emp_id
                     AND tsd.ts_date BETWEEN TO_DATE ('01-04-2012',
                                                      'dd-MM-yyyy'
                                                     )
                                         AND TO_DATE ('30-04-2012',
                                                      'dd-MM-yyyy'
                                                     ))) AS day11
    FROM emp_mst empmst
   WHERE TSD.EMP_ID=EMPMST.EMP_ID
ORDER BY empmst.emp_id

我想将外部查询的EMPMST.EMP_ID传递给内部查询,但内部查询不会在where子句中获取EMPMST.EMP_ID。 请告诉我如何将外部查询从表传递到内部查询。 内部查询如下

    (SELECT RTRIM
            (XMLAGG (XMLELEMENT (e, d || ',')).EXTRACT ('//text()').EXTRACT
                                                                   ('//text()'),
             ','
            )
   FROM (SELECT TO_DATE ('01-04-2012', 'dd-MM-yyyy') - 1 + ROWNUM AS d
           FROM all_objects
          WHERE TO_DATE ('01-04-2012', 'dd-MM-yyyy') - 1 + ROWNUM <=
                                          TO_DATE ('30-04-2012', 'dd-MM-yyyy')
         MINUS
         SELECT tsd.ts_date
           FROM emp_mst empmst, ts_dtl tsd
          WHERE empmst.emp_id = tsd.emp_id
            AND ts_date BETWEEN TO_DATE ('01-04-2012', 'dd-MM-yyyy')
                            AND TO_DATE ('30-04-2012', 'dd-MM-yyyy')))

我想要emp_name和emp_id以及TS_date,其中TS_DATE在4月的TS_DTL中不存在意味着4月剩余的时间在TS_DTL表中不存在

1 个答案:

答案 0 :(得分:0)

请在下面找到相同的

SELECT EMPMST.EMP_ID,
       EMPMST.EMP_NAME,
       rtrim(xmlagg(xmlelement(e, b.d1 || ',')).extract('//text()')
         .extract('//text()'),
         ',') as day11
  from (SELECT TO_DATE('01-04-2012', 'dd-MM-yyyy') - 1 + rownum AS d1
      FROM all_objects
     WHERE TO_DATE('01-04-2012', 'dd-MM-yyyy') - 1 + rownum <=
           TO_DATE('30-04-2012', 'dd-MM-yyyy')) b,
       EMP_MST EMPMST
 WHERE EMPMST.EMAIL_ID IS NOT NULL
   AND EMPMST.DEPT IN ('Technical')
   AND EMPMST.EMP_STATUS_LKP_ID = 201
   AND b.d1 NOT IN
       (SELECT TSD.TS_DATE as d2
      FROM TS_DTL TSD
     WHERE TSD.TS_DATE BETWEEN TO_DATE('01-04-2012', 'dd-MM-yyyy') AND
           TO_DATE('30-04-2012', 'dd-MM-yyyy')
       AND TSD.EMP_ID = EMPMST.EMP_ID)
 ORDER BY EMPMST.EMP_ID