如何为以下查询添加序列号

时间:2011-01-03 09:57:43

标签: sql-server

如何为以下查询添加序列号。我希望最终结果与序列号一起显示。

select
  EP.FirstName
, Ep.LastName
, [dbo].[GetBookingRoleName](ES.UserId,EP.BookingRole) as RoleName
from [3rdi_EventParticipants] as EP
inner join [3rdi_EventSignup] as ES on EP.SignUpId = ES.SignUpId
where EP.EventId =13
and userid in (
select distinct userid from userroles
where roleid not in(19, 20, 21, 22) and roleid not in(1,2))

1 个答案:

答案 0 :(得分:1)

以下是基于名字标记 serial 行号的查询:

SELECT Row_number() OVER(ORDER BY (SELECT 1))  AS 'Row Number', 
       ep.firstname, 
       ep.lastname, 
       [dbo].[Getbookingrolename](es.userid, ep.bookingrole) AS rolename 
FROM   [3rdi_EventParticipants] AS ep 
       INNER JOIN [3rdi_EventSignup] AS es 
         ON ep.signupid = es.signupid 
WHERE  ep.eventid = 13 
       AND userid IN (SELECT DISTINCT userid 
                      FROM   userroles 
                      WHERE  roleid NOT IN( 19, 20, 21, 22 ) 
                             AND roleid NOT IN( 1, 2 )) 

编辑:删除了EP.FirstName,Ep.LastName

上的ORDER BY