select
t.*
from
表 t
where
not exists(select 1 from 表 where str=t.str and key<t.key)
order by [t.time]
t.*
from
表 t
where
not exists(select 1 from 表 where str=t.str and key<t.key)
order by [t.time]
解决方案 »
- 请教sqlserver中能否在一个事物里面开事物
- 求该处where条件如何写
- 求一个sqlserver存储过程【help~】
- 数据库备份问题:企业版的数据库备份不能还原到个人版?重装以后不能进行数据库还原?
- 我本机的SQLSERVER2000查询分析器和企业管理器已打开,CPU占用率就到100%不变了,怎么回事啊?
- 在SQLserver中,怎么把多行数值加起来给另外一行?
- 要定期重复一个操作,触发器有没有这个功能!!十万火急
- ID值可以人工修改吗?
- 表里有多条记录,我想一次只取一条出来,然后处理完毕后,系统自动取下一条数据,用SQL怎么实现。“很急的问题”
- 有关DELETE触发器的问题?祝各位新年好!请邦我!
- 请高手解决一个sql的写法!
- !!!!!!帮我看看如何处理啊
from 表
group by str
t.*
from
表 t
where
not exists(select 1 from 表 where [str]=t.[str] and [key]<t.[key])
order by [t.time]
t.*
from
表 t
where
not exists(select 1 from 表 where [str]=t.[str] and [key]<t.[key])
order by t.[time]
t.Key ,t.Str, t.Time
from
表 t
where
Time=(select min(time) from 表 where t.Str=str)
一直是空啊
from table1
group by Str
order by min(Time)不就行了?
+-----------------------------------------------------+
| 学无止境....
+-----------------------------------------------------+
where [str]=t.[str] and [key]<t.[key]想想吧,你这条件永远不成立,就跟 1<1 是一样的效果取唯一直有两种办法 要么 distinct ,要么 group by +-----------------------------------------------------+
| 学无止境....
+-----------------------------------------------------+
insert into @t select 1,'a','2005-11-1'
union all select 2,'a','2005-11-2'
union all select 3,'b','2005-11-2'
union all select 4,'b','2005-11-3'select * from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1 Str Time
----------- ---------- ------------------------------------------------------
1 a 2005-11-01 00:00:00.000
3 b 2005-11-02 00:00:00.000 */
你可以这样理解
for str,time in (select * from @t a )
if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
OUTPUT
end if;
next你可以把str,time的数据代到循环中就明白了
第一次循环a,'2005-11-1' not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
第二次循环a,'2005-11-2' not exists ( select * from @t b where a=b.str and 2005-11-2<b.c )
....
第N次循环.....
declare @t table(Key1 int,Str varchar(10),Time datetime)
insert into @t select 1,'a','2005-11-1'
union all select 2,'a','2005-11-2'
union all select 3,'b','2005-11-2'
union all select 4,'b','2005-11-3'select * from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1 Str Time
----------- ---------- ------------------------------------------------------
1 a 2005-11-01 00:00:00.000
3 b 2005-11-02 00:00:00.000 */
你可以这样理解
for str,time in (select * from @t a )
if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
OUTPUT
end if;
next你可以把str,time的数据代到循环中就明白了
第一次循环a,'2005-11-1' not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
第二次循环a,'2005-11-2' not exists ( select * from @t b where a=b.str and 2005-11-2<b.time )
....
第N次循环.....
上面笔误
declare @t table(Key1 int,Str varchar(10),Time datetime)
insert into @t select 1,'a','2005-11-1'
union all select 2,'a','2005-11-2'
union all select 3,'b','2005-11-2'
union all select 4,'b','2005-11-3'select * from @t t where not exists (select * from @t b where t.str=b.str and t.time>b.time)/*Key1 Str Time
----------- ---------- ------------------------------------------------------
1 a 2005-11-01 00:00:00.000
3 b 2005-11-02 00:00:00.000 */
你可以这样理解
for str,time in (select * from @t a )
if ( not exists ( select * from @t b where a.str=b.str and a.time>b.time ) ) then
OUTPUT
end if;
next你可以把str,time的数据代到循环中就明白了
第一次循环a,'2005-11-1' not exists ( select * from @t b where a=b.str and 2005-11-1>b.time )
第二次循环a,'2005-11-2' not exists ( select * from @t b where a=b.str and 2005-11-2>b.time )
....
第N次循环.....