序号从1开始

Serial NO starting from 1

我有一个查询,其中我想要一个列作为 SR_No 应该从 1

开始

下面是我的查询

SELECT         
top 2      
 userName,   
 Party_name, 
 Ref_No,
 Ref_date,   
 doc_date,
 Last_Action_date,
 RAName,          
 COUNT(Doc_No) AS CountofDocNo,                  
 Document_Type,           
 RA1_Email                  
FROM #MainTempTable         
 GROUP BY RAName,          
 userName,           
 Document_Type,          
 RA1_Email ,Party_name,Ref_No,
 Ref_date, doc_date,Last_Action_date
 order by RAName

如何从 1 开始序列号

我试过 Row_NUM() 但它不是从 1 开始的。

更新

我尝试了 Madhivnan 的解决方案,但它对我的第二个查询不起作用

SELECT         
top 2  
 row_number() over (order by (select 0)) as SR_No,      
 UserName,
 RAName,
 Party_Name,     
 Ref_No,
 Ref_date, 
 doc_date,   
 Last_Action_date,
 Document_Type,           
 Doc_No,           
 No_Of_Days_Doc_Pending,           
 UserEmail,RA1_Email                  
 FROM #MainTempTable    order by UserName   

它应该适用于您的情况:

SELECT         
top 2      
 userName,   
 Party_name, 
 Ref_No,
 Ref_date,   
 doc_date,
 Last_Action_date,
 RAName,          
 COUNT(Doc_No) AS CountofDocNo,                  
 Document_Type,           
 RA1_Email,
 ROW_NUMBER() OVER(ORDER BY RAName) AS SR_No                 
FROM #MainTempTable         
 GROUP BY RAName,          
 userName,           
 Document_Type,          
 RA1_Email ,Party_name,Ref_No,
 Ref_date, doc_date,Last_Action_date

试试这个

SELECT         
top 2 row_number() over (order by (select 0)) as SR_No,     
 userName,   
 Party_name, 
 Ref_No,
 Ref_date,   
 doc_date,
 Last_Action_date,
 RAName,          
 COUNT(Doc_No) AS CountofDocNo,                  
 Document_Type,           
 RA1_Email                  
FROM #MainTempTable         
 GROUP BY RAName,          
 userName,           
 Document_Type,          
 RA1_Email ,Party_name,Ref_No,
 Ref_date, doc_date,Last_Action_date    
order by RAName

这个可以帮到你

select  ROW_NUMBER() OVER ( order by field1) as rownumber,field2,field3 from dbo.yourtable