简体   繁体   中英

query condition - Oracle SQL

I wrote code only on transact sql . I want to write this query in oracle sql. But it doesn't work . the main problem that I can't write my select parameters in the where condition . How to avoid this problem . My query ,

 select t.*, 
 count(*) over(partition by t.el1_code) cnt ,
 count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
 concat(t.el1_name,t.el1_sname) str,
 max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
 from tasuser.coda_element_1 t
 )tt


 —where tt.cnt>1 and (tt.el1_valid=0 or (tt.el1_valid=1 and cnt=cnt1 and     
  str<>str_check) )
  where not  (tt.cnt>1 and (tt.el1_valid=0 or (tt.el1_valid=1 and    
  cnt=cnt1       and 
  str<>str_check)))

You need to use derived table/inline view:

SELECT *
FROM (
 select t.*, 
 count(*) over(partition by t.el1_code) cnt ,
 count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
 concat(t.el1_name,t.el1_sname) str,
 max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
 from tasuser.coda_element_1 t
 )  tt
WHERE tt.conditions....

or common table expression :

WITH cte AS (
select t.*, 
     count(*) over(partition by t.el1_code) cnt ,
     count(*) over(partition by t.el1_code, t.el1_valid) cnt1 ,
     concat(t.el1_name,t.el1_sname) str,
     max(concat(t.el1_name,t.el1_sname)) over(partition by t.el1_code) str_check
     from tasuser.coda_element_1 t

)
SELECT *
FROM cte
WHERE conditions ...

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