查询中以周数形式的序列号

时间:2012-10-16 21:02:43

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

我用它来获取每周不同RepID的序列号和数量。但不是序列号(1,2,3),我希望序列号值为第1周,第2周,第3周。怎么可能?这是使用SQL Server 2005.谢谢!

Select ROW_NUMBER() OVER (ORDER BY Date) As SlNo, count(distinct(RepID))
from Reptable   
where Month(Date) = @Month
group by Datepart(week, Date)

1 个答案:

答案 0 :(得分:0)

您需要做的只是将cast() row_number作为字符串,然后在其开头连接Week

Select 
  'Week ' + cast(ROW_NUMBER() OVER (ORDER BY Date) as varchar(10)) As SlNo, 
  count(distinct(RepID))
from Reptable   
where Month(Date) = @Month
group by Datepart(week, Date)