简体   繁体   中英

Partition Function Count() over

I'm trying to write the following in order to get a running total of WO_Count, like so: I have to keep condition like WO_Count > 1. Could any one help me please? If I keep where WO_Count > 1, I'm getting below error:

ORA-00904: "WO_COUNT": invalid identifier 00904. 00000 - "%s: invalid identifier"

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  

You need to nest your query, either using a Derived Table or a Common Table Expression:

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

Btw, your count doesn't return a running total , but a group count .

To get a running count you can use the ROW_NUMBER() analytic function:

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 )

(Note: you could also use COUNT(*) OVER ( PARTITION BY ... ORDER BY ... ) to get a running count but when you are counting rows, using * , they are equivalent.)

If you want all the rows after the first then:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM