Sql server SQL窗口函数的rank或rownum出现问题

Sql server SQL窗口函数的rank或rownum出现问题,sql-server,function,rank,Sql Server,Function,Rank,我希望以下数据的输出如下所示: id empid question type rownum 1 75 How old are you SSS 1 2 75 NULL LLL 2 3 88 How old are you SSS 1 4 88 NULL LLL

我希望以下数据的输出如下所示:

   id   empid   question            type    rownum
  1      75     How old are you     SSS       1
  2      75     NULL                LLL       2
  3      88     How old are you     SSS       1
  4      88     NULL                LLL       2
  5      99     How old are you     SSS       1
  6      99     How old are you     LLL       1
  7      99     NULL                LLL       2
我想要的输出是,如果有一个empid同时具有SSS和LLL类型,但LLL问题为null,那么只返回SSS记录。在某些情况下,empid既有带问号的SSS类型,也有带问号的LLL类型,但也包含带空问号的第二个LLL类型。最后,我将只带回rownum=1的记录

    declare @t table(id int, empid int, question varchar(250), type char(3))
    insert into @t values( 1, 75, 'How old are you', 'SSS'),
                  (2, 75, NULL, 'LLL'),
                  (3, 88, 'How old are you', 'SSS'),
                  (4, 88, NULL, 'LLL'),
                  (5, 99, 'How old are you', 'SSS'),
                  (6, 99, 'How old are you', 'LLL'),
                  (7, 99, NULL, 'LLL')
我编写了下面的查询,结果很接近,但是我无法得到我想要的确切结果,因为它为empid=99的两种“LLL”类型都分配了rownum=2

   select *,
   rank()over(partition by empid order by type desc,
   case when question is not null and type = 'LLL' then 1 else 2 end desc) as rownum
   from @t
我想我明白了:

     select *,
     dense_rank()over(partition by empid, case when question is not null and type = 
     'LLL' then 1 else 2 end order by type desc
      ) as rownum
       from @t
我想我明白了:

     select *,
     dense_rank()over(partition by empid, case when question is not null and type = 
     'LLL' then 1 else 2 end order by type desc
      ) as rownum
       from @t

你可以接受你自己的答案。