/**********RANK() 函数与 ROW_NUMBER() 对现有数据排序的改造 ***************/
-- select left('claro',2) 整理于西安, -06-10 12:18:17.687
--> Microsoft SQL Server 2005 - 9.00.1406.00
-- (Intel X86) Mar 3 2007 18:40:02 Copyright (c) 1988-2005
--Microsoft Corporation Enterprise Edition on Windows NT 5.2 (Build 3790: Service Pack 2)
-- 在现有数据中生成排序数据
SELECT userid= ROW_NUMBER() OVER ( ORDER BY 日期 ), 编号 , 日期 , 时间
FROM (
select 1 编号 , '20090601' 日期 , '0701' 时间
union all select 1, '20090601' , '1130'
union all select 1, '20090601' , '1400'
union all select 1, '20090601' , '1530' ) a
/*
userid 编号 日期 时间
1 1 20090601 0701
2 1 20090601 1130
3 1 20090601 1400
4 1 20090601 1530
*/
-- 在现有数据中分区生成排序数据(一)
SELECT userid= RANK() OVER ( ORDER BY 时间 ), 编号 , 日期 , 时间 -- 按时间分区(不重复)生成排序数据
FROM (
select 1 编号 , '20090601' 日期 , '0701' 时间
union all select 1, '20090601' , '1130'
union all select 1, '20090601' , '1400'
union all select 1, '20090601' , '1530' ) a
/*
userid 编号 日期 时间
1 1 20090601 0701
2 1 20090601 1130
3 1 20090601 1400
4 1 20090601 1530
*/
-- 在现有数据中分区生成排序数据(二)
SELECT userid= RANK() OVER ( ORDER BY 日期 ), 编号 , 日期 , 时间 -- 按日期分区(重复)生成排序数据
FROM (
select 1 编号 , '20090601' 日期 , '0701' 时间
union all select 1, '20090601' , '1130'
union all select 1, '20090601' , '1400'
union all select 1, '20090601' , '1530'
union all select 1, '20090602' , '1550' ) a
/*
userid 编号 日期 时间
1 1 20090601 1130
1 1 20090601 1400
1 1 20090601 1530
1 1 20090601 0701
5 1 20090602 1550
*/