得到每个STATIONID的前两条是不?
一条SQL不好实现吧.
我感觉得整个存储过程
一条SQL不好实现吧.
我感觉得整个存储过程
解决方案 »
- sql server 2000没有数据库级触发器?
- 谁有SQLServer2005数据库分表的知识?
- 求一删除语句
- charindex如何处理的?
- 200分求 分页思路疑问 1000万条记录发布读取内存错误
- 计算平均值的问题
- 频繁读取数据库
- SQL Server 派生列的问题
- 在中文版的查询分析器中可以可执行的update语句, 在英文版中不能执行?为什么?出错信息如下
- 在SQL SERVER中,设置某一个表的一列为IDENTITY INT,同时设此列为主键,在使用中,插入新记录时,出现错误‘插入重复主键值’,原因是SQL SERVER系统表记忆的IDENTITY列的值变小,请问怎样可直接修改此系统表?
- 排序问题
- 数据库被我误删了,求大虾指教如何恢复!!急急急,分数不够再加!
select * from 表 a
where [time] in(select top 2 [time] from 表 where STATIONID=a.STATIONID)
create table 表(STATIONID int,TIME datetime,ICVALUE int,CVALUE int)
insert 表 select 2520,'2003/04/28 22:07:48',-3514,31
union all select 2520,'2003/04/28 22:13:49',-36,34
union all select 2520,'2003/04/28 22:41:35',-4248,46
union all select 2520,'2003/04/28 22:50:28',-3847,1176
union all select 2530,'2003/04/28 23:02:10',-3208,3
union all select 2530,'2003/04/28 23:15:10',-57,18
union all select 2530,'2003/04/28 23:15:57',-3599,1303
union all select 2530,'2003/04/28 23:20:31',-4436,22
union all select 2540,'2003/04/28 23:38:05',-4655,425
union all select 2540,'2003/04/28 23:51:42',-3907,24
union all select 2540,'2003/04/29 00:02:28',-4228,14
union all select 2540,'2003/04/28 23:02:28',-4226,12
go--查询
select * from 表 a
where [time] in(select top 2 [time] from 表 where STATIONID=a.STATIONID)
go--删除测试
drop table 表/*--测试结果STATIONID TIME ICVALUE CVALUE
----------- --------------------------- ----------- ---------
2520 2003-04-28 22:07:48.000 -3514 31
2520 2003-04-28 22:13:49.000 -36 34
2530 2003-04-28 23:02:10.000 -3208 3
2530 2003-04-28 23:15:10.000 -57 18
2540 2003-04-28 23:38:05.000 -4655 425
2540 2003-04-28 23:51:42.000 -3907 24(所影响的行数为 6 行)
--*/
select *
into #t
from 表
where TIME not in(select min(TIME) from 表 group by STATIONID)
order by STATIONID,TIMEselect *
from 表
where TIME in(select min(TIME) from 表 group by STATIONID)
union
select *
from #t
where TIME in(select min(TIME) from #t group by STATIONID)
order by STATIONID,TIME drop table #t
UNION ALL
select top 2 * from 表 where STATIONID = '2530'
UNION ALL
select top 2 * from 表 where STATIONID = '2540'或者SELECT * from 表 a where ICVALUE IN(select top 2 ICVALUE from 表 where STATIONID =a.STATIONID )