合并三个查询的结果并在另一个查询中使用它们

时间:2018-08-08 11:11:54

标签: sql oracle oracle11g

我要使用以下三个查询:

第一个查询:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastissue 
FROM 
    AMICOS.HISTORYSTOCKFLOAT 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID 
WHERE 
    (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='ISSUE') AND    
    ((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1)) 
GROUP BY 
    AMICOS.PNRREG.PN;

第二个查询:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORY.HISTORYDATE) AS lastissuehistory
FROM
    AMICOS.HISTORY 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORY.PARTID = AMICOS.PNRREG.PARTID
WHERE 
   (((AMICOS.HISTORY.HISTORYACTION)='ISSUE') AND 
    ((AMICOS.HISTORY.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY 
    AMICOS.PNRREG.PN;

第三次查询:

SELECT 
    AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastpurchase
FROM
    AMICOS.HISTORYSTOCKFLOAT 
INNER JOIN 
    AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID
WHERE 
    (((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY 
    AMICOS.PNRREG.PN, AMICOS.HISTORYSTOCKFLOAT.ACTION
HAVING 
    (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='PURCHASE'));

最后一个查询将它们组合在一起:

SELECT AMICOS.PNRREG.PN, AMICOS.IRCABCCAT.ABC_CATEGORY, AMICOS.IRC.PRIMUTILISATION, AMICOS.PNRREG.DESCRIPTION, AMICOS.HISTORYSTOCKFLOAT.ACCOUNTNO, AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE, AMICOS.HISTORYSTOCKFLOAT.STOCK_FLOAT, AMICOS.HISTORYSTOCKFLOAT.STOCK_PRICE, AMICOS.HISTORYSTOCKFLOAT.STOCKVALUE, lastpurchase.lastpurchase, lastissue.lastissue, lastissuehistory.lastissuehistory
FROM ((AMICOS.PNRREG LEFT JOIN lastissuehistory ON AMICOS.PNRREG.PN = lastissuehistory.PN LEFT JOIN lastissue ON AMICOS.PNRREG.PN = lastissue.PN) 
LEFT JOIN lastpurchase ON AMICOS.PNRREG.PN = lastpurchase.PN);

我尝试了UNION,JOIN将这些查询放在一起,以从最后三个查询中获得一个结果,这是其他三个查询的依存关系。

当我对前三个查询有预定义的查询时,它可用于MS Access。但这不适用于Oracle SQL Developer查询工具。

我该如何实现?

4 个答案:

答案 0 :(得分:2)

看起来像WITH(公用表表达式,即with first as (select pn, ... from ...), second as (select pn, ... from ...), third as (select pn, ... from ...) -- now, join them select f.pn, s.some_column, t.some_other_column from first f join second s on s.pn = f.pn join third t on t.pn = f.pn 分解子句)可能会有所帮助。像这样:

    <td pEditableColumn>
       <p-cellEditor>
      <ng-template pTemplate="input">
        <input type="text" [(ngModel)]="rowData.vin" (keyup.enter)="onKeyPress($event)">
      </ng-template>
      <ng-template pTemplate="output">
        {{rowData.vin}}
      </ng-template>
    </p-cellEditor>
  </td>

答案 1 :(得分:0)

您可以使用With或以下选项

SELECT 
    AMICOS.PNRREG.PN, 
    AMICOS.IRCABCCAT.ABC_CATEGORY, 
    AMICOS.IRC.PRIMUTILISATION, 
    AMICOS.PNRREG.DESCRIPTION, 
    AMICOS.HISTORYSTOCKFLOAT.ACCOUNTNO, 
    AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE, 
    AMICOS.HISTORYSTOCKFLOAT.STOCK_FLOAT, 
    AMICOS.HISTORYSTOCKFLOAT.STOCK_PRICE, 
    AMICOS.HISTORYSTOCKFLOAT.STOCKVALUE, 
    lastpurchase.lastpurchase, 
    lastissue.lastissue, 
    lastissuehistory.lastissuehistory
FROM 
    AMICOS.PNRREG

LEFT JOIN (SELECT 
AMICOS.PNRREG.PN, 
Max(AMICOS.HISTORY.HISTORYDATE) AS lastissuehistory
FROM 
    AMICOS.HISTORY 
    INNER JOIN AMICOS.PNRREG ON AMICOS.HISTORY.PARTID = AMICOS.PNRREG.PARTID
WHERE (((AMICOS.HISTORY.HISTORYACTION)='ISSUE') AND ((AMICOS.HISTORY.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY AMICOS.PNRREG.PN) lastissuehistory ON AMICOS.PNRREG.PN = lastissuehistory.PN 

LEFT JOIN (SELECT 
AMICOS.PNRREG.PN, 
Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastissue 
FROM 
    AMICOS.HISTORYSTOCKFLOAT 
    INNER JOIN AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID 
WHERE 
(((AMICOS.HISTORYSTOCKFLOAT.ACTION)='ISSUE') AND ((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1)) 
GROUP BY AMICOS.PNRREG.PN)lastissue ON AMICOS.PNRREG.PN = lastissue.PN

LEFT JOIN (SELECT 
    AMICOS.PNRREG.PN, 
    Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastpurchase
FROM 
    AMICOS.HISTORYSTOCKFLOAT 
    INNER JOIN AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID
WHERE (((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY AMICOS.PNRREG.PN, AMICOS.HISTORYSTOCKFLOAT.ACTION
HAVING (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='PURCHASE'))) lastpurchase ON AMICOS.PNRREG.PN = lastpurchase.PN

答案 2 :(得分:0)

@小脚丫 我尝试过这样的事情:

WITH
first as (SELECT AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastissue FROM AMICOS.HISTORYSTOCKFLOAT INNER JOIN AMICOS.PNRREG ON                   AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID WHERE (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='ISSUE') AND                                           ((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1)) GROUP BY AMICOS.PNRREG.PN),
second as (SELECT AMICOS.PNRREG.PN, Max(AMICOS.HISTORY.HISTORYDATE) AS lastissuehistory
       FROM AMICOS.HISTORY INNER JOIN AMICOS.PNRREG ON AMICOS.HISTORY.PARTID = AMICOS.PNRREG.PARTID
       WHERE (((AMICOS.HISTORY.HISTORYACTION)='ISSUE') AND ((AMICOS.HISTORY.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
       GROUP BY AMICOS.PNRREG.PN),
third as (SELECT AMICOS.PNRREG.PN, Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE) AS lastpurchase
FROM AMICOS.HISTORYSTOCKFLOAT INNER JOIN AMICOS.PNRREG ON AMICOS.HISTORYSTOCKFLOAT.PARTID = AMICOS.PNRREG.PARTID
WHERE (((AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)<to_date('31032019','DDMMYYYY')+1))
GROUP BY AMICOS.PNRREG.PN, AMICOS.HISTORYSTOCKFLOAT.ACTION
HAVING (((AMICOS.HISTORYSTOCKFLOAT.ACTION)='PURCHASE')))
SELECT AMICOS.PNRREG.PN, AMICOS.IRCABCCAT.ABC_CATEGORY, AMICOS.IRC.PRIMUTILISATION,
AMICOS.PNRREG.DESCRIPTION, AMICOS.HISTORYSTOCKFLOAT.ACCOUNTNO,
AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE, AMICOS.HISTORYSTOCKFLOAT.STOCK_FLOAT,
AMICOS.HISTORYSTOCKFLOAT.STOCK_PRICE, AMICOS.HISTORYSTOCKFLOAT.STOCKVALUE,
lastpurchase.lastpurchase, lastissue.lastissue, lastissuehistory.lastissuehistory
FROM ((AMICOS.PNRREG LEFT JOIN lastpurchase ON AMICOS.PNRREG.PN = lastpurchase.PN
LEFT JOIN lastissue ON AMICOS.PNRREG.PN = lastissue.PN) LEFT JOIN lastissuehistory ON
AMICOS.PNRREG.PN = lastissuehistory.PN;

但是它一直告诉我“选择列表与GROUP BY不一致;将GROUP BY子句修改为:AMICOS.PNRREG.PN,AMICOS.HISTORYSTOCKFLOAT.ACTION,Max(AMICOS.HISTORYSTOCKFLOAT.HISTORYDATE)”

问候 Erneeraq

答案 3 :(得分:0)

我在以下指南的帮助下成功完成了这项工作:Use lookupset function in SSRS

熊凯瑟(Katherine Xiong)的建议。

致谢

Erneeraq