Oracle:subselect中的标识符无效

时间:2016-11-11 15:42:08

标签: sql oracle subquery

我正在尝试使用子选择从关联表中获取最后一行,但似乎子选择不知道外部查询中涉及的一个表,错误是kd.KPI_DEF_ID,无效标识符。用数字替换它会使查询有效。

SELECT bp.bp_id,
     kd.kpi_def_id,
     kd.kpi_name,
     ks.kpi_status_now,
     kd.threshold_min_val,
     kd.threshold_max_val,
     kd.threshold_min_alert,
     kd.threshold_max_alert,
     e.event_id,
     e.event_name,
     (SELECT *
      FROM   (SELECT l.log_desc
            FROM   rator_monitoring.alert_logs l
            WHERE  l.kpi_def_id = kd.kpi_def_id
            ORDER  BY TIMESTAMP DESC)
      WHERE  rownum = 1) log_desc
FROM   business_process bp
JOIN   kpi_definition kd
ON     (kd.bp_id = bp.bp_id)
JOIN   rator_monitoring.kpi_status ks
ON     (ks.kpi_def_id = kd.kpi_def_id)
JOIN   event e
ON     (e.event_id = kd.event_id)
WHERE  kd.kpi_active_current = 'Y';

我该怎么做才能解决这个问题?

2 个答案:

答案 0 :(得分:3)

您需要转换log_desc标量子查询,以便在顶层完成相关联接。例如,像:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace App3
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
    }
}

N.B。未经测试。

答案 1 :(得分:1)

UNTESTED:使用内联视图重新构建为联接;虽然不确定性能......

SELECT bp.bp_id,
     kd.kpi_def_id,
     kd.kpi_name,
     ks.kpi_status_now,
     kd.threshold_min_val,
     kd.threshold_max_val,
     kd.threshold_min_alert,
     kd.threshold_max_alert,
     e.event_id,
     e.event_name,
     l.log_desc
FROM   business_process bp
JOIN   kpi_definition kd
  ON   kd.bp_id = bp.bp_id
JOIN   rator_monitoring.kpi_status ks
  ON   ks.kpi_def_id = kd.kpi_def_id
JOIN   event e
  ON   e.event_id = kd.event_id
LEFT JOIN (SELECT log_Desc
        FROM rator_monitoring.alert_logs 
        INNER JOIN (SELECT max(timestamp) mts, kpi_def_ID 
                    FROM  rator_monitoring.alert_logs 
                    GROUP BY kpi_def_ID) Z
          ON Z.mts = l.timestamp
         AND Z.kpi_def_ID = l.kpi_def_ID) l
  ON l.kpi_def_id = kd.kpi_def_id
WHERE  kd.kpi_active_current = 'Y';