对于sql server中的每一行,行号始终返回1

时间:2013-07-03 12:20:47

标签: row-number

(SELECT 
    (SELECT ROW_NUMBER() OVER (order by t.NotificationID)) as  RowNumber,
            [NotificationID],[ProjectID],[TeamMemberID],[OperationType],
            [Hours],[Occurance],[Period],[NotificationText],
            [NotificationRecipientIDs],[NotificationRecipientClienitsIDs]

       FROM tblIA_Notifications t
       WHERE IsDeleted = 0 AND IsActive = 1
    ) 

以上查询始终为每行返回rownumber 1。当我在外面使用select语句时,它的问题。否则,如果我删除外部select语句就可以了。

我不明白这种行为。

2 个答案:

答案 0 :(得分:2)

你们每行得到row_number 1,bcoz正在为每一行选择Row_Number 试试这个--->

SELECT        ROW_NUMBER() OVER (order by t.NotificationID) as RowNumber,
              [NotificationID],
              [ProjectID],
              [TeamMemberID],
              [OperationType],
              [Hours],
              [Occurance],
              [Period],
              [NotificationText],
              [NotificationRecipientIDs],
              [NotificationRecipientClienitsIDs]
    FROM      tblIA_Notifications    t 
    WHERE     IsDeleted    =    0 
    AND       IsActive = 1

答案 1 :(得分:1)

试试吧......

SELECT ROW_NUMBER() OVER (order by T.COLUMN_NAME) as RowNumber FROM [dbo].[TABLE_NAME] T