按照时间自动编号(SQL)

原来问题:
select * from tb where 
结果如下:
time num
9:01 23
9:02 1.2
9:03 112
9:04 3
  ..

希望得到如下结果:
id time num
1  9:01 23
2  9:02 1.2
3  9:03 112
4  9:04 3
5    ..
不用临时表.
解决方法1:
select *,(select count(*from  tb where a.time>=time ) from tb a where
解决方法2:
select (select count(1from tb b  where a.time>=b.time) as ID, * from tb a
后来想一下,想到另外一种解决方法:
CREATE TABLE #T([time] nvarchar(5) ,num float)
INSERT INTO #T
SELECT '9:01'23 UNION ALL
SELECT '9:02' ,1.2 UNION ALL
SELECT '9:03' ,112 UNION ALL
SELECT '9:04' ,3

SELECT T2.[id],T1.* FROM #T AS T1
INNER JOIN 
 (
SELECT A.[time],SUM(1AS [id]  
  
FROM #T AS A
  
INNER JOIN #T AS B ON B.[time]<=A.[time]
  
GROUP BY A.[time]
 ) 
AS T2 ON T2.[time]=T1.[time]
DROP TABLE #T
/*
id time num
1  9:01 23
2  9:02 1.2
3  9:03 112
4  9:04 3
*/


完。
原帖:http://community.csdn.net/Expert/topic/5606/5606978.xml?temp=.4288446
原文地址:https://www.cnblogs.com/wghao/p/788088.html