2013-03-07 30 views
0

我有下面的代碼:如何給在記錄計數where子句

select b_id, 
     process_date 
    from (select c1.b_id, 
       c1.process_date, 
       row_number() over(partition by a.a_id 
           order by c1.process_date desc) rn 
      from table_a a 
       inner join 
       table_b b 
       on a.a_id = b.a_id 
       inner join 
       table_c c1 
       on b.b_id = c1.b_id 
     ) 
where rn = 1; 

如何分配RN動態即RN =計數(RN).Tried,它是givng錯誤,指出沒有一組由功能

回答

1

您不能在where子句中使用count。使用group by和having。

如:

SELECT columnname, COUNT(*) 
FROM TableName 
GROUP BY columnname 
HAVING COUNT(*) = 1 
0

您可以使用count(*)的解析函數:

select b_id, 
     process_date 
    from (select c1.b_id, 
       c1.process_date, 
       row_number() over(partition by a.a_id 
           order by c1.process_date desc) rn, 
       count(*) over()         cn 
      from table_a a 
       inner join 
       table_b b 
       on a.a_id = b.a_id 
       inner join 
       table_c c1 
       on b.b_id = c1.b_id 
     ) 
where rn = cn; 

的情況下,你只是想獲得最後process_date其足以排序的子查詢:

select b_id, 
      process_date 
     from (select c1.b_id, 
        c1.process_date,     
       from table_a a 
        inner join 
        table_b b 
        on a.a_id = b.a_id 
        inner join 
        table_c c1 
        on b.b_id = c1.b_id 
       order by c1.process_date desc 
      ) 
     where rownum = 1; 

where rn = cn;