查询条件 - Oracle SQL

时间:2017-08-23 15:32:36

标签: sql oracle

我只在transact sql上编写代码。我想在oracle sql中编写这个查询。但它不起作用。我无法在where条件下编写select参数的主要问题。如何避免这个问题。我的查询,

 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)))

1 个答案:

答案 0 :(得分:0)

您需要使用派生表/内联视图:

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....

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 ...