您的位置:首页 > 数据库

按照时间自动编号(SQL)

2007-06-18 18:54 218 查看
原来问题:

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(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

完。
原帖:http://community.csdn.net/Expert/topic/5606/5606978.xml?temp=.4288446
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: