0

最初のクエリと 2 番目のクエリを LEFT JOIN する必要がある 2 つのクエリがあります。目的は、これらすべてを別の大きなものの中にラップすることです。最初のクエリと 2 番目のクエリの両方が単独で動作するようになりましたが、それらを結合させることはできません。

最初のクエリ:

  SELECT *
  FROM  (
    SELECT Source as system, DT as ts, Status as statusCode
    FROM (
      (SELECT 'SOURCE1' Source FROM Dual
       UNION SELECT 'SOURCE2' FROM Dual
       UNION SELECT 'SOURCE3' FROM Dual
       UNION SELECT 'SOURCE4' FROM Dual
       ) system
      CROSS JOIN (
        SELECT
            TO_DATE('09-30-2013','MM-DD-YYYY') - 1 + LEVEL dt
        FROM dual
            CONNECT BY
        LEVEL <= ( TO_DATE('10/05/2013','MM/DD/YYYY')
                 - TO_DATE('09/30/2013','MM/DD/YYYY')) + 1
       ) ts
       CROSS JOIN (
        SELECT 'O' Status FROM Dual
        UNION SELECT 'C' FROM Dual
       ) statusCode
    )--For some reason cannot name this so need to wrap in another select *
  )Duals

2 番目のクエリ: (LEFT JOIN があります) ここの間に

LEFT JOIN

試された

  Select * FROM(
    SELECT myTable1.system, TO_CHAR(maxResults.ts,'YYYY-MM-DD') as ts, myTable1.statusCode
    FROM (
      SELECT table_id, MAX(ts) as ts
         FROM myTable1_history
         WHERE ts BETWEEN TO_TIMESTAMP('2013-09-29','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-06','yyyy-mm-dd') 
         GROUP BY table_id )maxResults
    JOIN myTable1
    ON maxResults.table_id = myTable1.table_id
    WHERE myTable1.statusCode = 'C'
  UNION ALL
    SELECT myTable1.system as "system", TO_CHAR(myTable1.ts,'YYYY-MM-DD') as "ts", 'O' as "statusCode" 
    FROM myTable1
    WHERE myTable1.ts BETWEEN TO_TIMESTAMP('2013-09-29','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-06','yyyy-mm-dd') 
    --AND   myTable1.statusCode = 'O'
    )Records

USING (system, ts, statusCode)

2 つのクエリの途中で LEFT JOIN に固執しようとしましたが、示されているように機能しませんでした (おそらく間違っています)。

編集:「無効なテーブル名」を受け取り、機能していないものの例としてJOINとUSINGを追加しました

4

1 に答える 1

0

これは、すべての列で結合したいと仮定した場合の推測ですか?

(SELECT * FROM (
  SELECT system, ts, statuscode
   FROM  (SELECT Source as system, DT as ts, Status as statusCode
                FROM ((SELECT 'SOURCE1' Source FROM Dual
                            UNION SELECT 'SOURCE2' FROM Dual
                            UNION SELECT 'SOURCE3' FROM Dual
                            UNION SELECT 'SOURCE4' FROM Dual
                 ) system CROSS JOIN (SELECT TO_DATE('09-30-2013','MM-DD-YYYY') - 1 + LEVEL dt
                                                                    FROM dual
                                                                CONNECT BY
                                                                    LEVEL <= ( TO_DATE('10/05/2013','MM/DD/YYYY')
                                                                             - TO_DATE('09/30/2013','MM/DD/YYYY')) + 1
                                                                       ) ts CROSS JOIN (SELECT 'O' Status FROM Dual
                                                                                                     UNION SELECT 'C' FROM Dual) statusCode
    )
  ))duals LEFT JOIN 
  (Select * FROM(
    SELECT myTable1.system, TO_CHAR(maxResults.ts,'YYYY-MM-DD') as ts, myTable1.statusCode
    FROM (
      SELECT table_id, MAX(ts) as ts
         FROM myTable1_history
         WHERE ts BETWEEN TO_TIMESTAMP('2013-09-29','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-06','yyyy-mm-dd') 
         GROUP BY table_id )maxResults
    JOIN myTable1
    ON maxResults.table_id = myTable1.table_id
    WHERE myTable1.statusCode = 'C'
  UNION ALL
    SELECT myTable1.system, TO_CHAR(myTable1.ts,'YYYY-MM-DD') as ts, 'O' as statusCode 
    FROM myTable1
    WHERE myTable1.ts BETWEEN TO_TIMESTAMP('2013-09-29','yyyy-mm-dd') AND TO_TIMESTAMP('2013-10-06','yyyy-mm-dd') 
    --AND   myTable1.statusCode = 'O'
    ) Records ON duals.system = records.system AND duals.ts = records.ts AND duals.statusCode = records.statusCode
于 2013-10-14T15:46:50.767 に答える