有点麻烦,不过还是能够实现,首先找出每个featureid对应的最大detailid,然后再根据detailid 找到所谓的第一个userid,然后featureid,userid的分组中选择最小的detailidselect e.featureid,d.userid, min(e.detailid) from request d, detail e,
( select b.featureid, a.userid from request a, detail b,
  (select featureid, Max(detailid) max_detailid from detail group by featureid ) c
  where a.requestid = b.requestid and b.featureid=c.featureid and b.detailid =  c.max_detailid ) f
  where d.userid = f.userid and d.requestid=e.requestid and e.featureid = f.featureid
  group by e.featureid,d.userid

解决方案 »

  1.   

    To :Lastdrop(空杯) 
    非常感谢你的回复,但好像结果还是不对的
      你选出来的最小detailId是整个FeatureId分组中最小的了!
    我的表达水平实在是对不起大家其实题目的意思是很简单的,但选择真是不好选,实在不行就拥游标了,但还是不甘心
    我估计Lastdrop(空杯) 是看懂我意思了,可是结果好像是错的group by e.featureid,d.userid 和Group by e.FeatueId 在这里好像是没什么区别吧我想了一下 可能要用个having(...) ,但头大了
    想不动了
      

  2.   

    看看这个。我只是写了一下,没有经过测试。
    select yyy.featureid,xxx.userid,max(yyy.detailid)
    from request xxx , detail yyy, (select b.featureid,b.detailid,row_number() over(partition by b.featureid order by b.detailid desc) as num,a.userid
    from request a , detail b
    where a.requestid=b.requestid) zzz
    where xxx.requestid=yyy.requestid
      and xxx.userid = zzz.userid
      and zzz.num=1
      and zzz.detailid=yyy.detailid
    group by yyy.featureid,xxx.userid
      

  3.   

    好像不对,试一下这个。唉,我也头大了
    select yyy.featureid,xxx.userid,max(yyy.detailid)
    from request xxx , detail yyy, (select b.featureid,b.detailid,row_number() over(partition by b.featureid order by b.detailid desc) as num,a.userid
    from request a , detail b
    where a.requestid=b.requestid) zzz
    where xxx.requestid=yyy.requestid
      and xxx.userid = zzz.userid
      and zzz.num=1
    group by yyy.featureid,xxx.userid
      

  4.   

    我明白意思了,上面是三个分组,要求根据输入的userId,找是否是分组中的第一条,如果是,查接着的是不是,直到不是为止,在这些数据中间找到detailid最小的,而不是找分组中最小的;如果不是,查下一个分组,用游标作比较方便,我去做实验
      

  5.   

    真不容易,终于搞定!再加一个判断,不过劝你以后最好不要产生这种需求 :)
    select e.featureid,d.userid, min(e.detailid) from request d, detail e,
    ( select b.featureid, a.userid from request a, detail b,
      (select featureid, Max(detailid) max_detailid from detail group by featureid ) c
      where a.requestid = b.requestid and b.featureid=c.featureid and b.detailid =  c.max_detailid ) f
      where d.userid = f.userid and d.requestid=e.requestid and e.featureid = f.featureid
            and e.detailid > ( select max(detailid) from detail g where g.featureid = f.featureid
            and g.requestid <> d.requestid )            
      group by e.featureid,d.userid
      

  6.   

    SQL> select a.featureid,b.userid,min(detailid) from 
      2  detail a,request b,
      3  (select featureid,userid from detail a, request b where a.requestid=b.requestid
      4  and detailid in (select max(detailid) from detail group by featureid)) c
      5  where a.requestid=b.requestid and b.userid=c.userid
      6  and a.featureid=c.featureid
      7  and detailid>(select max(detailid) from detail c,request d where 
      8  featureid=a.featureid and c.requestid=d.requestid and d.userid<>b.userid
      9  group by featureid)
     10  group by a.featureid,b.userid; FEATUREID     USERID MIN(DETAILID)
    ---------- ---------- -------------
             1          1            99
             2          1           200
             3          5           300搞定,呵呵
      

  7.   

    上句还可以精简,有些没意义的语句
    SQL> select a.featureid,b.userid,min(detailid) from 
      2  detail a,request b,
      3  (select featureid,userid from detail a, request b where a.requestid=b.requestid
      4  and detailid in (select max(detailid) from detail group by featureid)) c
      5  where a.requestid=b.requestid and b.userid=c.userid
      6  and a.featureid=c.featureid
      7  and detailid>(select max(detailid) from detail c,request d where 
      8  featureid=a.featureid and c.requestid=d.requestid and d.userid<>b.userid)
      9   group by a.featureid,b.userid; FEATUREID     USERID MIN(DETAILID)
    ---------- ---------- -------------
             1          1            99
             2          1           200
             3          5           300
      

  8.   

    商业逻辑上确实比较复杂,一般都用一段程序,怎么现在都流行一句SQL语句了?呵呵,这样的程序代码量少了,可惜运行效率不一定高啊!
      

  9.   

    select e.featureid,d.userid, min(e.detailid) from request d, detail e,
    ( select b.featureid, a.userid from request a, detail b,
      (select featureid, Max(detailid) max_detailid from detail group by featureid ) c
      where a.requestid = b.requestid and b.featureid=c.featureid and b.detailid =  c.max_detailid ) f
      where d.userid = f.userid and d.requestid=e.requestid and e.featureid = f.featureid
            and e.detailid > ( select max(detailid) from detail g where g.featureid = f.featureid
            and g.requestid <> d.requestid )            
      group by e.featureid,d.userid