序列号从1开始

时间:2016-07-27 06:25:40

标签: sql sql-server sql-server-2005

我有一个查询,其中我想要一列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开始序列号。

我尝试使用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 ,但它不是从1开始。

更新

我尝试了Madhivnan的解决方案,但它对我的第二次查询没有用

public class myInterceptor extends EmptyInterceptor{

private static final long serialVersionUID = 1L;
Session session;

public void setSession(Session session) {
    this.session=session;
}

public boolean onSave(Object entity,Serializable id,
    Object[] state,String[] propertyNames,Type[] types)
    throws CallbackException {
    System.out.println("onSave");   
    return false;   
}

public boolean onFlushDirty(Object entity,Serializable id,
    Object[] currentState,Object[] previousState,
    String[] propertyNames,Type[] types)
    throws CallbackException {
    System.out.println("onFlushDirty"); 
    return false;
}

public void onDelete(Object entity, Serializable id, 
    Object[] state, String[] propertyNames, 
    Type[] types) { 
    System.out.println("onDelete");     
}

//called before commit into database
public void preFlush(Iterator iterator) {
    System.out.println("preFlush");
}   

//called after committed into database
public void postFlush(Iterator iterator) {
    System.out.println("postFlush");    
   }        
}

3 个答案:

答案 0 :(得分:2)

试试这个

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

答案 1 :(得分:0)

它应该适合你的情况:

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

答案 2 :(得分:0)

这个可以帮到你

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