0

我有以下代码:

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=count(rn) .Tried 并给出错误说不是按功能分组

4

2 回答 2

1

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

例如:

SELECT columnname, COUNT(*)   
FROM TableName
GROUP BY columnname
HAVING COUNT(*) = 1
于 2013-03-07T13:32:48.083 回答
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;
于 2013-03-07T14:42:12.313 回答