【问题标题】:Oracle Top 5 Problems of Top 5 SitesOracle Top 5 站点的 5 大问题
【发布时间】:2021-02-24 10:53:23
【问题描述】:

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

我有两个语句分别做得很好

声明 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, objectid, CTINCI
FROM   ordered_query
WHERE  rownum <= 5

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

WITH ordered_query AS
  (SELECT debitorid, objectid, errorcode, count(incident) as CTINCI
   FROM   ssrs_tblx_sla STSLA
   WHERE Debitorid = :Debitor 
   and   objectid = :Objectid 
   group by debitorid, objectid, errorcode
   ORDER BY count(Incident) DESC, debitorid)
SELECT debitorid, objectid, errorcode, 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, 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, objectid, CTINCI
        FROM   ordered_query
        WHERE  rownum <= 5
        ) T5S
LEFT JOIN
        (WITH ordered_query AS
            (SELECT debitorid, objectid, errorcode, count(incident) as CTINCI
            FROM   ssrs_tblx_sla STSLA
            WHERE Debitorid = T5S.Debitorid 
            and   objectid = T5S.Objectid 
            group by debitorid, objectid, errorcode
            ORDER BY count(Incident) DESC, debitorid)
        SELECT debitorid, objectid, errorcode, 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:无效标识符”
*原因:
*行动:
Zeile 的 Fehler:20 斯帕尔特:56

【问题讨论】:

    标签: oracle join parameters row-number with-statement


    【解决方案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
      ),
    t5s as
      (select debitorid, objectid, ctinci
         from ordered_query
        where rownum <= 5
      ),    
    ordered_query_2 as
      (select debitorid, objectid, 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, objectid, errorcode
        order by count(incident) desc, debitorid)
      ),
    t5p as
      (select debitorid, objectid, errorcode, 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-27
      • 2014-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多