似乎没有正确的行号,我可以在模板相同时计算它,但如果模板更改则无法继续计数
create table #temp
(
question varchar(max)
,rvdt datetime
,name varchar(20)
,cdt datetime
,template varchar(max)
)
insert into #temp
(question ,rvdt ,name ,cdt ,template)
values
('question 1', '11/11/2014 10:56:27 PM', 'Joe', '10/11/2014 11:56:27 PM', 'Tempalte 1'),
('question 2', '11/11/2014 10:56:27 PM', 'Joe', '10/11/2014 11:56:27 PM', 'Tempalte 1'),
('question 3', '11/11/2014 10:56:27 PM', 'Joe', '10/11/2014 11:56:27 PM', 'Tempalte 1'),
('question 11', '11/12/2014 10:56:27 PM', 'Joe', '10/12/2014 11:56:27 PM', 'Tempalte 2'),
('question 22', '11/12/2014 10:56:27 PM', 'Joe', '10/12/2014 11:56:27 PM', 'Tempalte 2'),
('question 32', '11/12/2014 10:56:27 PM', 'Joe', '10/12/2014 11:56:27 PM', 'Tempalte 2'),
('question 1', '11/13/2014 10:56:27 PM', 'Joe', '10/13/2014 11:56:27 PM', 'Tempalte 1'),
('question 2', '11/13/2014 10:56:27 PM', 'Joe', '10/13/2014 11:56:27 PM', 'Tempalte 1'),
('question 3', '11/13/2014 10:56:27 PM', 'Joe', '10/13/2014 11:56:27 PM', 'Tempalte 1'),
('question 11', '11/14/2014 10:56:27 PM', 'Mike', '10/14/2014 11:56:27 PM', 'Tempalte 2'),
('question 22', '11/14/2014 10:56:27 PM', 'Mike', '10/14/2014 11:56:27 PM', 'Tempalte 2'),
('question 33', '11/14/2014 10:56:27 PM', 'Mike', '10/14/2014 11:56:27 PM', 'Tempalte 2')
Select
ROW_NUMBER() over (partition by name, rvdt order by rvdt )'rv#'
,rvdt
,cdt
,name
,template
from
#temp
order by rvdt
drop table #temp
我尝试了不同的组合,但是我似乎找不到合适的组合
答案 0 :(得分:3)
使用Dense_Rank()
Ranking Function
SELECT Dense_rank()OVER (partition BY name ORDER BY cdt) Rn,
rvdt,
cdt,
name,
template
FROM #temp
输出:
Rn rvdt cdt name template
-- ----------------------- ----------------------- ---- ----------
1 2014-11-11 22:56:27.000 2014-10-11 23:56:27.000 Joe Tempalte 1
1 2014-11-11 22:56:27.000 2014-10-11 23:56:27.000 Joe Tempalte 1
1 2014-11-11 22:56:27.000 2014-10-11 23:56:27.000 Joe Tempalte 1
2 2014-11-12 22:56:27.000 2014-10-12 23:56:27.000 Joe Tempalte 2
2 2014-11-12 22:56:27.000 2014-10-12 23:56:27.000 Joe Tempalte 2
2 2014-11-12 22:56:27.000 2014-10-12 23:56:27.000 Joe Tempalte 2
3 2014-11-13 22:56:27.000 2014-10-13 23:56:27.000 Joe Tempalte 1
3 2014-11-13 22:56:27.000 2014-10-13 23:56:27.000 Joe Tempalte 1
3 2014-11-13 22:56:27.000 2014-10-13 23:56:27.000 Joe Tempalte 1
1 2014-11-14 22:56:27.000 2014-10-14 23:56:27.000 Mike Tempalte 2
1 2014-11-14 22:56:27.000 2014-10-14 23:56:27.000 Mike Tempalte 2
1 2014-11-14 22:56:27.000 2014-10-14 23:56:27.000 Mike Tempalte 2