按照时间自动编号(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
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
相关文章推荐
- SQL2000中生成带自动编号的新表的sql语句.
- SQL怎样自动生成编号格式如:BV+YYYYMM+编号
- [学习]利用SqlDataAdapter Insertcommand 获取刚新增的自动编号ID值
- sql加一列自动编号
- PostgreSQL中使用动态SQL-实现自动按时间创建表分区
- 推荐SQL Server 重新恢复自动编号列的序号的sql代码
- M| SQL 导入导出的时候数据库表的主键和自动编号丢失 怎么办
- 如何在SQL中设置自动编号收藏
- sql 自动生成编号函数
- Access转成Sql 2008步骤,同时解决自动编号问题,主键,id数值不重置。
- word中的表格怎么按照章节自动插入题注(即表头的编号)
- Word 2007第n级编号不自动按照父级标题自动编号 的解决办法
- SQL自动编号
- 数据库排名sql,group by 分组查询按照时间最大值
- sql语句自动编号
- 数据库排名sql,group by 分组查询按照时间最大值
- 如何在SQL中设置自动编号
- 小议SQL_Server主键和自动编号问题
- SQL-利用默认自动记录时间
- sql自动生成{00N}编号