日期:2014-05-17  浏览次数:20807 次

讨论找到某数据集行前数据集的总数
我有一个表A,含有N列,现在我对表查询:
select   *   from   A   where   a.n1   >   ?   and   a.n2   >   ?   order   a.n3   desc

会得到一个数据结果,那么我需要知道此数据集中的某数据行前后多少行,最好的办法是什么呢?

我的办法是:
假设此数据行的id=N,那么

select   ((select   count(*)   from   A   where   a.n1   >   ?   and   a.n2   >   ?   and   a.n3   >   (select   n3   from   A   where   id=N)   order   a.n3   desc)   +   (select   count(*)   from   A   where   a.n1   >   ?   and   a.n2   >   ?   and   a.n3   =   (select   n3   from   A   where   id=N)   and   a.id   >   N   order   a.id   desc))

欢迎大家来讨论

------解决方案--------------------
可借用伪列rownum实现