product_id      user_id 
 223344          123 
 223344          234 
 223344          334 
 223344          222
 225566          441
 225566          443
 225566          444 
 225566          445       
取product_id对应的 3条不同的user_id的记录。
假如porduct_id没有对应的user_id ,则输出为空值.

解决方案 »

  1.   

    row_number()分组排序取前三行  
      

  2.   

    select product_id, user_id from ( 
     select t.*, row_number() over (partition by product_id order by user_id) rn from t
    ) where rn <= 3;
      

  3.   

    这个能不能实现一个product_id必定对应3条user_id, 即使没有user_id..查询出来的记录也是输出空值?
      

  4.   


    with t as(
     select 223344 product_id,123 user_id from dual union all  
     select 223344,234 from dual union all  
     select 223344,334 from dual union all  
     select 223344,222 from dual union all
     select 225566,441 from dual union all
     select 225566,443 from dual union all
     select 225566,443 from dual union all 
     select 225566,445 from dual union all
     select 225567,446 from dual union all
     select 225568,447 from dual union all
     select 225569,448 from dual
     )
     select t4.product_id,t3.user_id from 
     (select t1.product_id,t1.user_id,num2 from 
          (select t.product_id,t.user_id,row_number() over(partition by product_id,user_id order by user_id) num1,
                                         dense_rank() over(partition by product_id order by user_id) num2  from t)t1
        where t1.num1 = 1 and t1.num2 <= 3)t3,
     (select distinct product_id,level num2 from t connect by level <=3)t4
     where t3.product_id(+) = t4.product_id and t3.num2(+) = t4.num23 223344 234
    2 223344 222
    1 223344 123
    6 225566 445
    5 225566 443
    4 225566 441
    13 225567
    15 225567
    7 225567 446
    14 225568
    11 225568
    8 225568 447
    9 225569 448
    12 225569
    10 225569 取product_id对应的 3条不同的user_id的记录。
    假如porduct_id没有对应的user_id ,则输出为空值.
      

  5.   

    用分析函数,以product_id 为partition by ,以user_id 为order by 。
    目的是先以每个product_id 为分组,在后面增加序列号。
    然后再依据序列号进行选择就行了。