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:结果如下:
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 ..
不用临时表.
select *,(select count(*) from tb where a.time>=time ) from tb a where
解决方法2:select (select count(1) from 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(1) AS [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
*/
完。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(1) AS [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