ORA-00904“无效标识符”,但标识符存在于查询

时间:2015-12-17 09:17:18

标签: sql oracle identifier

我正在使用故障报告Oracle数据库,试图从中获取故障信息。

我要查询的主表是事件,其中包括事件信息。事件中的每条记录都可以在WorkOrder表中有任意数量的记录(或者没有),并且WorkOrder中的每条记录在WorkLog表中都可以有任意数量的记录(或者没有)。

此时我要做的是,对于Incident中的每条记录,在字段MXRONSITE中找到具有最小值的WorkLog,并且对于该工作日志,从工作订单返回MXRONSITE时间和REPORTDATE。我使用MIN子查询完成了这个,但事实证明,几个工作日志可能具有相同的MXRONSITE时间,因此我收回的记录超出了我想要的数量。我试图为它创建一个子查询,但它现在说我在WHERE行中有一个WOL1.WONUM的无效标识符(ORA-00904),即使该标识符在其他地方使用过。

感谢任何帮助。请注意,查询中还有其他内容,但查询的其余部分是孤立的,但这不能在完整查询中使用,也不能单独使用。

SELECT    
      WL1.MXRONSITE as "Date_First_Onsite",
      WOL1.REPORTDATE as "Date_First_Onsite_Notified"
FROM Maximo.Incident
LEFT JOIN (Maximo.WorkOrder WOL1
           LEFT JOIN Maximo.Worklog WL1
                  ON WL1.RECORDKEY = WOL1.WONUM) 
       ON WOL1.ORIGRECORDID = Incident.TICKETID 
          AND WOL1.ORIGRECORDCLASS = 'INCIDENT'
WHERE (WL1.WORKLOGID IN 
       (SELECT MIN(WL3.WORKLOGID) 
        FROM (SELECT MIN(WL3.MXRONSITE), WL3.WORKLOGID  
              FROM Maximo.Worklog WL3 WHERE WOL1.WONUM = WL3.RECORDKEY)) 
       or WL1.WORKLOGID is null)

澄清一下,我想要的是:

  • 对于事件中的每个故障,
  • Worklog表中最早的MXRONSITE(如果存在这样的值),
  • 对于该工作日志,来自WorkOrder表的关联记录的信息。

事故记录包含多个工作单,而且工单有多个工作日志,这可能具有相同的MXRONSITE时间,这很复杂。

经过一些试验,我找到了一个(几乎)可行的解决方案:

WITH WLONSITE as (
SELECT
MIN(WLW.MXRONSITE) as "ONSITE",
WLWOW.ORIGRECORDID as "TICKETID",
WLWOW.WONUM as "WONUM"
FROM
MAXIMO.WORKLOG WLW
INNER JOIN
MAXIMO.WORKORDER WLWOW
ON
WLW.RECORDKEY = WLWOW.WONUM
WHERE
WLWOW.ORIGRECORDCLASS = 'INCIDENT'
GROUP BY 
WLWOW.ORIGRECORDID, WLWOW.WONUM
)

select
incident.ticketid,
wlonsite.onsite,
wlonsite.wonum
from
maximo.incident

LEFT JOIN WLONSITE
ON WLONSITE.TICKETID = Incident.TICKETID

WHERE 
(WLONSITE.ONSITE is null or WLONSITE.ONSITE = (SELECT MIN(WLONSITE.ONSITE) FROM WLONSITE WHERE WLONSITE.TICKETID = Incident.TICKETID AND ROWNUM=1))
AND Incident.AFFECTEDDATE >= TO_DATE ('01/12/2015', 'DD/MM/YYYY')

然而这显然更慢,但仍然不太正确,因为事实证明,单个事件可以有多个具有相同ONSITE时间的工作订单(aaargh!)。

根据要求,这是一个示例输入,以及我想从中得到的内容(对格式化道歉)。请注意,虽然TICKETID和WONUM是主键,但它们是字符串而不是整数。 WORKLOGID是一个整数。

事故表:
TICKETID / Description / FieldX
1 / WORD1 / S
2 / WORD2 / P
3 / WORDX /
4 / / Q

工单表:
WONUM / ORIGRECORDID / REPORTDATE
11/1 / 2015-01-01
12/2 / 2015-01-01
13/2 / 2015-02-04
14/3 / 2015-04-05

工作日志表:
WORKLOGID / RECORDKEY / MXRONSITE
101/11 / 2015-01-05
102/12 / 2015-01-04
103/12 /
104/12 / 2015-02-05
105/13 /

输出:
TICKETID / WONUM / WORKLOGID
1/11 / 101
2/12 / 102
3 / / 4 / /
(与TICKETID 1相关联的工作日志101,具有非零MXRONSITE,来自工单11) (与TICKETID 2相关联的工作日志102-105,其中102具有最低的MXRONSITE,并且是工作单12) (没有与故障103或104关联的工作日志,因此工作单和工作日志字段为空)

发布圣诞节攻击!

我找到了一个有效的解决方案:

我发现的方法是使用多个WITH查询,如下所示:

WLMINL AS (
  SELECT 
  RECORDKEY, MXRONSITE, MIN(WORKLOGID) AS "WORKLOG"
  FROM MAXIMO.WORKLOG
  WHERE WORKLOG.CLASS = 'WORKORDER'
  GROUP BY RECORDKEY, MXRONSITE
),
WLMIND AS (
  SELECT
  RECORDKEY, MIN(MXRONSITE) AS "MXRONSITE"
  FROM MAXIMO.WORKLOG
  WHERE WORKLOG.CLASS = 'WORKORDER'
  GROUP BY RECORDKEY
),
WLMIN AS (
  SELECT
  WLMIND.RECORDKEY AS "WONUM", WLMIND.MXRONSITE AS "ONSITE",   WLMINL.WORKLOG AS "WORKLOGID"
  FROM
  WLMIND
  INNER JOIN
  WLMINL
  ON
  WLMIND.RECORDKEY = WLMINL.RECORDKEY AND WLMIND.MXRONSITE =   WLMINL.MXRONSITE
)

因此,对于找到第一个日期的每个工单,然后对于找到最低工作日志的每个工单和日期,然后加入两个表。然后在更高级别重复此操作以按事件查找数据。

然而,这种方法在合理的时间内不起作用,因此虽然它可能适用于较小的数据库,但对于我正在使用的庞然大物来说并不好。

3 个答案:

答案 0 :(得分:0)

嵌套子查询时,无法访问属于两个或更多级别的列;在您的语句中,最内层子查询中无法访问WL1。 (还有一个分组缺失,顺便说一句)

这可能有效(不完全确定您期望的输出,但尝试一下):

SELECT

WL1.MXRONSITE as "Date_First_Onsite",
WOL1.REPORTDATE as "Date_First_Onsite_Notified"

FROM Maximo.Incident

LEFT JOIN (
  Maximo.WorkOrder WOL1
  LEFT JOIN Maximo.Worklog WL1
  ON WL1.RECORDKEY = WOL1.WONUM
) ON WOL1.ORIGRECORDID = Incident.TICKETID 
AND WOL1.ORIGRECORDCLASS = 'INCIDENT'

WHERE WL1.WORKLOGID =
  ( SELECT MIN(WL3.WORKLOGID)
      FROM Maximo.WorkOrder WOL3
      LEFT JOIN Maximo.Worklog WL3
        ON WL3.RECORDKEY = WOL3.WONUM
      WHERE WOL3.ORIGRECORDID = WOL1.ORIGRECORDID
        AND WL3.MXRONSITE IS NOT NULL
  )
  OR WL1.WORKLOGID IS NULL AND NOT EXISTS 
     ( SELECT MIN(WL4.WORKLOGID)
         FROM Maximo.WorkOrder WOL4
         LEFT JOIN Maximo.Worklog WL4
           ON WL4.RECORDKEY = WOL4.WONUM
         WHERE WOL4.ORIGRECORDID = WOL1.ORIGRECORDID
           AND WL4.MXRONSITE IS NOT NULL )

答案 1 :(得分:0)

我可能没有关于你要做什么的详细信息...如果你有一些样本输入和所需的输出,那将是一个很大的帮助。

那就是说,我认为分析函数可以提供很多帮助,不仅可以获得输出,还可以组织代码。以下是如何使用子查询中的with wo as ( select wonum, origrecordclass, origrecordid, reportdate, max (reportdate) over (partition by origrecordid) as max_date from Maximo.workorder where origrecordclass = 'INCIDENT' ), logs as ( select worklogid, mxronsite, recordkey, max (mxronsite) over (partition by recordkey) as max_mx from Maximo.worklog ) select i.ticketid, l.mxronsite as "Date_First_Onsite", wo.reportdate as "Date_First_Onsite_Notified" from Maximo.incident i left join wo on wo.origrecordid = i.ticketid and wo.reportdate = wo.max_date left join logs l on wo.wonum = l.recordkey and l.mxronsite = l.max_mx 分析函数的示例。

同样,关于加入的详细信息可能已经关闭 - 如果您可以提供一些示例输入和输出,我敢打赌有人可以到达您想去的地方:

with wo_logs as (
  select
    wo.wonum, wo.origrecordclass, wo.origrecordid, wo.reportdate,
    l.worklogid, l.mxronsite, l.recordkey,
    max (reportdate) over (partition by origrecordid) as max_date,
    min (mxronsite) over (partition by recordkey) as min_mx
  from
    Maximo.workorder wo
    left join Maximo.worklog l on wo.wonum = l.recordkey
  where wo.origrecordclass = 'INCIDENT'
)
select
  i.ticketid, wl.wonum, wl.worklogid,
  wl.mxronsite as "Date_First_Onsite",
  wl.reportdate as "Date_First_Onsite_Notified"
from
  Maximo.incident i
  left join wo_logs wl on
    i.ticketid = wl.origrecordid and
    wl.mxronsite = wl.min_mx
order by 1

- 编辑 -

根据您的样本输入和所需输出,这似乎可以提供所需的结果。它确实在子查询中发生了一些爆炸,但希望分析函数的效率会抑制它。与使用group by相比,它们通常要快得多:

class Instance : IDisposable
{
    //something
    void Dispose()
    {
        //Disposes something
    }

}

答案 2 :(得分:0)

我会用row_number函数执行此操作:

SQLFiddle

select ticketid, case when worklogid is not null then reportdate end d1, mxronsite d2
  from (
    select i.ticketid, wo.reportdate, wl.mxronsite, wo.wonum, wl.worklogid,
           row_number() over (partition by i.ticketid 
                                  order by wl.mxronsite, wo.reportdate) rn
      from incident i 
        left join workorder wo on wo.origrecordid = i.ticketid 
                              and wo.origrecordclass = 'INCIDENT'
        left join worklog wl   on wl.recordkey = wo.wonum )
  where rn = 1 order by ticketid