带子查询的最后一个id

时间:2016-11-01 21:20:37

标签: database oracle plsql

我有以下pl / sql查询:

select praca.nmpraca, pista.idpista, sentidoPista.NMSENTIDOPISTA, pista.idslt, statusSltPista.STATUS, statusSltPista.DHMMENSAGEM,
      statusSltProxy.DHMMENSAGEM, statusSltProxy.STATUS, statusAntena.DHMMENSAGEM, statusAntena.STATUS, statusMcr.DHMMENSAGEM, statusMcr.STATUS,
      chavesMcr.DHM_RECEBIDO, chavesMcr.QNT_CHAVES, statusPista.DHMMENSAGEM, statusPista.STATUS
FROM  MCR_TBPRACA praca
inner 
join  MCR_TBINATIVACAOPISTA pista
on    praca.IDTBPRACA = pista.IDPRACA
inner 
join  MCR_TBSENTIDOPISTA sentidoPista 
ON    sentidoPista.IDSENTIDOPISTA = pista.IDSENTIDOPISTA
left 
join  MCR_TBSTATUSGEACOMSLT statusSltPista
on    statusSltPista.IDPRACA = praca.IDPRACA
and   pista.IDPISTA = statusSltPista.PISTA
and   statusSltPista.IDSTATUSGEACOMSLT = (select max(s.IDSTATUSGEACOMSLT)      from MCR_TBSTATUSGEACOMSLT s where s.praca = praca.IDPRACA and s.pista = pista.IDPISTA) 
left 
join MCR_TBSTATUSLANEALIVE statusSltProxy
on   statusSltProxy.IDPISTA = pista.IDPISTA
and  statusSltProxy.IDPRACA = praca.IDPRACA
left 
join MCR_TBSTATUSANTENA statusAntena
on   statusAntena.IDPISTA = pista.IDPISTA
and  statusAntena.IDPRACA = praca.IDPRACA
left 
join MCR_TBSTATUSMCRCOMPUTADOR statusMcr
on   statusMcr.IDPISTA = pista.IDPISTA
and  statusMcr.IDPRACA = praca.IDPRACA
and  statusMcr.IDSLT = pista.IDSLT
left
join MCR_TBCHAVESMCR chavesMcr 
on   chavesMcr.IDSLT = pista.IDSLT
left 
join MCR_TBSTATUSPISTA statusPista
on   statusPista.IDPRACA = praca.IDPRACA
and  statusPista.IDPISTA = pista.IDPISTA

我需要获取表MCR_TBSTATUSGEACOMSLT(别名statusSltPista)的最后一个寄存器,所以我用max来做那个子查询。但是,我收到了这个错误:

  
      
  1. 00000 - "列可能没有外部连接到子查询"   *原因:不允许(+)()。   *操作:删除(+)或从子查询中查看视图。          在V6和之前,在这种情况下,(+)被忽略了。
  2.   

我明白它告诉我的是什么,但我真的不知道该怎么做。

有什么想法吗?

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以将其设为内联视图的JOIN

而不是:

left join MCR_TBSTATUSGEACOMSLT statusSltPista
on statusSltPista.IDPRACA = praca.IDPRACA
and pista.IDPISTA = statusSltPista.PISTA
and statusSltPista.IDSTATUSGEACOMSLT = (select max(s.IDSTATUSGEACOMSLT) from MCR_TBSTATUSGEACOMSLT s where s.praca = praca.IDPRACA and s.pista = pista.IDPISTA)

你可以这样做:

left join (
    select IDPRACA, PISTA, max(IDSTATUSGEACOMSLT) as Max_IDSTATUSGEACOMSLT
    from MCR_TBSTATUSGEACOMSLT
    group by IDPRACA, PISTA
) statusSltPista
on statusSltPista.IDPRACA = praca.IDPRACA
and statusSltPista.PISTA = pista.IDPISTA

请务必在查询的其他位置正确引用“max”列 - 使用上面的示例,该值为statusSltPista.Max_IDSTATUSGEACOMSLT