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)。試行しましたが、groupby関数ではないと言ってエラーが発生します。

4

2 に答える 2

1

where 句で count を使用することはできません。group by と having を使用します。

例えば:

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 に答える