列出最近回复帖子的前两名不同的用户
主题号 用户名 回复时间
1 a 9:00
1 b 9:30
2 b 10:40
2 c 10:50
2 a 11:00
2 a 11:20
3 c 12:30
每个主题都要前两名,按要求,结果集为:
主题号 用户名 回复时间
1 b 9:30
1 a 9:00
2 a 11:20
2 c 10:50
3 c 12:30该SQL语句该怎么写阿??

解决方案 »

  1.   

    select 主题号,用户名,回复时间 
    from (
    select 主题号,用户名,回复时间,row_number() over(partition by 主题号,用户名 order by 回复时间 desc) rn
    from table)
    where rn=1;
      

  2.   

    恩,只显示两个用户,上面全显示了。
    改:
    select 主题号,用户名,回复时间
    from (
    select 主题号,用户名,回复时间,row_number() over(partition by 主题号 order by 回复时间 desc) rn1
    from (
    select 主题号,用户名,回复时间
    from (
    select 主题号,用户名,回复时间,row_number() over(partition by 主题号,用户名 order by 回复时间 desc) rn
    from t)
    where rn=1
    order by 主题号,回复时间 desc))
    where rn1<=2
      

  3.   


    SQL> drop table t_temp;Table droppedSQL> 
    SQL> CREATE TABLE t_temp (
      2     subject_id   VARCHAR2(10),
      3     USERs        VARCHAR2(10),
      4     r_time       DATE
      5     )
      6  ;Table createdSQL> 
    SQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('1', 'a', to_date('17-04-2009 09:37:58', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('2', 'a', to_date('17-04-2009 09:38:06', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('2', 'a', to_date('17-04-2009 09:38:40', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('1', 'b', to_date('17-04-2009 09:38:54', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('2', 'b', to_date('17-04-2009 09:41:11', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('2', 'b', to_date('17-04-2009 09:41:16', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('1', 'a', to_date('17-04-2009 09:56:31', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> insert into T_TEMP (SUBJECT_ID, USERS, R_TIME)
      2  values ('1', 'a', to_date('17-04-2009 10:03:39', 'dd-mm-yyyy hh24:mi:ss'));1 row insertedSQL> 
    SQL> SELECT SUBJECT_ID, USERS, R_TIME
      2    FROM (SELECT SUBJECT_ID,
      3                 USERS,
      4                 R_TIME,  --根据SUBJECT_ID, USERS 分组
      5                 ROW_NUMBER() OVER(PARTITION BY SUBJECT_ID ORDER BY R_TIME DESC) RN
      6            FROM t_temp)
      7   WHERE RN <= 2;SUBJECT_ID USERS      R_TIME
    ---------- ---------- -----------
    1          a          2009-4-17 1
    1          a          2009-4-17 9
    2          b          2009-4-17 9
    2          b          2009-4-17 9SQL> 
      

  4.   

    看着有点晕乎,2楼的sql应该是正确的
      

  5.   

    select 主题号,用户名,回复时间
    from (
    select 主题号,用户名,回复时间,row_number() over(partition by 主题号 order by 回复时间 desc) rn1
    from (
    select 主题号,用户名,回复时间
    from (
    select 主题号,用户名,回复时间,row_number() over(partition by 主题号,用户名 order by 回复时间 desc) rn
    from t)
    where rn=1
    order by 主题号,回复时间 desc))
    where rn1<=2
    正解