Oracle Top 5 站点的 5 大问题

问题描述

大家好,我是这个版块的新手,之前我总是匿名查看,但现在我有一个特定的问题,我找不到答案。

我有两个语句,它们分别做的非常好

声明 1:选择问题最多的网站

WITH ordered_query AS
(SELECT debitorid,objectid,count(incident) as CTINCI
   FROM   ssrs_tblx_sla STSLA
   WHERE Debitorid = :Debitor 
   group by debitorid,objectid
   ORDER BY count(Incident) DESC,debitorid)
SELECT debitorid,CTINCI
FROM   ordered_query
WHERE  rownum <= 5

声明 2:网站的大多数错误代码

WITH ordered_query AS
  (SELECT debitorid,errorcode,count(incident) as CTINCI
   FROM   ssrs_tblx_sla STSLA
   WHERE Debitorid = :Debitor 
   and   objectid = :Objectid 
   group by debitorid,errorcode
   ORDER BY count(Incident) DESC,CTINCI
FROM   ordered_query
WHERE  rownum <= 5

在这两个语句中,我在 where 语句中使用了 oracle 参数。

现在我想合并它们并通过 LEFT JOIN 语句尝试此操作,但似乎我不能在语句 2 的 where 子句中使用语句 1 的值。

目标:我想获得事件数量最多的前 5 个站点,以及每个站点每个错误代码中事件数量最高的前 5 个站点

有人知道我做错了什么吗?我有点糊涂了

select  T5S.Debitorid,T5S.Objectid,T5S.CTINCI as Sitetotal,T5P.ERRORCODE,T5P.Ctini as ERCTotal
FROM
        (WITH ordered_query AS
            (SELECT debitorid,count(incident) as CTINCI
            FROM   ssrs_tblx_sla STSLA
            WHERE Debitorid = :Debitor 
            group by debitorid,objectid
            ORDER BY count(Incident) DESC,debitorid)
        SELECT debitorid,CTINCI
        FROM   ordered_query
        WHERE  rownum <= 5
        ) T5S
LEFT JOIN
        (WITH ordered_query AS
            (SELECT debitorid,count(incident) as CTINCI
            FROM   ssrs_tblx_sla STSLA
            WHERE Debitorid = T5S.Debitorid 
            and   objectid = T5S.Objectid 
            group by debitorid,errorcode
            ORDER BY count(Incident) DESC,CTINCI
        FROM   ordered_query
        WHERE  rownum <= 5
        ) T5P
ON      T5S.Debitorid = T5P.DebitorId and T5S.objectid=T5P.objectid

oracle 给我的错误代码是:

ORA-00904: "T5S"."OBJECTID": 无效标识符
00904. 00000 - "%s: 无效标识符"
*原因:
*行动:
泽勒的费勒:20 斯帕尔特:56

解决方法

对我来说,它看起来像这样(但我可能错了;无法测试,我没有你的表):

with 
ordered_query as
  (select debitorid,objectid,count(incident) as ctinci
     from ssrs_tblx_sla stsla
    where debitorid = :debitor 
    group by debitorid,objectid
    order by count(incident) desc,debitorid
  ),t5s as
  (select debitorid,ctinci
     from ordered_query
    where rownum <= 5
  ),ordered_query_2 as
  (select debitorid,errorcode,count(incident) as ctinci
     from ssrs_tblx_sla stsla join t5s on t5s.debitorid = stsla.debitorid
                                      and t5s.objectid = stsla.objectid
    group by debitorid,errorcode
    order by count(incident) desc,debitorid)
  ),t5p as
  (select debitorid,ctinci
     from ordered_query_2
    where rownum <= 5
  )
--
select  t5s.debitorid,t5s.objectid,t5s.ctinci as sitetotal,t5p.errorcode,t5p.ctini as erctotal,from t5s left join t5p t5s.debitorid = t5p.debitorid and t5s.objectid=t5p.objectid;