分区函数Count()结束

时间:2017-05-26 07:51:31

标签: sql oracle count partition

我试图编写以下内容以获得WO_Count的运行总计,如下所示: 我必须像WO_Count一样保持条件>请问有人帮我吗?如果我保持在哪里WO_Count> 1,我得到以下错误:

  

ORA-00904:" WO_COUNT":无效的标识符   00904. 00000 - "%s:无效的标识符"

select 
   Test1.id_number as Case_ID,
   count(*) over ( partition by Tes2.NAME ) as WO_Count
FROM Test1
INNER JOIN Test2 ON Test1.Test12Test2=Test2.ID  

2 个答案:

答案 0 :(得分:1)

您需要使用派生表或公用表表达式嵌套查询:

select *
from 
 (
    select 
       Test1.id_number as Case_ID,
       count(*) over ( partition by Tes2.NAME ) as WO_Count
    FROM Test1
    INNER JOIN Test2 ON Test1.Test12Test2=Test2.ID  
 ) dt
WHERE WO_Count > 1

顺便说一下,您的点数不会返回正在运行的总计,而是组计数

答案 1 :(得分:0)

要获得运行计数,您可以使用ROW_NUMBER()分析函数:

SELECT t1.id_number as Case_ID,
       ROW_NUMBER() OVER (
         PARTITION BY t2.NAME
         ORDER BY     ROWNUM  /* or a column name */
       ) as WO_Count
FROM   Test1 t1
       INNER JOIN Test2 t2
       ON ( t1.Test12Test2 = t2.ID )

(注意:您也可以使用COUNT(*) OVER ( PARTITION BY ... ORDER BY ... )获取正在运行的计数,但在使用*计算行数时,它们是等效的。)

如果你想要第一个之后的所有行:

SELECT *
FROM   (
  SELECT t1.id_number as Case_ID,
         ROW_NUMBER() OVER (
           PARTITION BY t2.NAME
           ORDER BY     ROWNUM  /* or a column name */
         ) as WO_Count
  FROM   Test1 t1
         INNER JOIN Test2 t2
         ON ( t1.Test12Test2 = t2.ID )
)
WHERE  WO_Count > 1