27,579
社区成员
发帖
与我相关
我的任务
分享
--括号错位
select *
from demo a
where [time] = (select min([time]) from demo where Num = a.Num and [Name] = a.[Name])
select *
from demo a
where [time] = (select min([time] from demo where Num = a.Num and [Name] = a.[Name]))
SELECT DISTINCT T.* FROM TB T,
(SELECT NUM,NAME,MIN(TIME)TIME FROM TB GROUP BY NUM,NAME) T1
WHERE T.NUM=T1.NUM
AND T.NAME=T1.NAME
select
*
from
demo t
where
not exists(select 1 from demo where num=t.num and [time]<t.[time])
select * from(select row_number() over(partition by num order by num) gid,* from tb) tmp where gid=1
select * from(select row_number() over(partition by mailrwid order by mailrwid) gid,* from pkeircltm0) tmp where gid=1
--處理時加一個輔助字段ID
declare @Tab table
(ID INT ,Num int, Name varchar(2), Time DATETIME)
insert into @tab select 1, 1 ,'a', '2009/05/01'
insert into @tab select 2, 1 ,'a', '2009/05/02'
insert into @tab select 3, 1 ,'a', '2009/05/03'
insert into @tab select 4,2 ,'b', '2009/05/04'
insert into @tab select 5,2 ,'b', '2009/05/05'
insert into @tab select 6,3 ,'c', '2009/05/06'
insert into @tab select 7,3 ,'c', '2009/05/07'
insert into @tab select 8,5 ,'e', '2009/05/08'
insert into @tab select 9,1 ,'a', '2009/05/09'
insert into @tab select 10,1 ,'a', '2009/05/10'
select * from @Tab t where name<>isnull((select top 1 isnull(name,'') from @tab where id=t.id-1 order by id ),'')
declare @Tab table
(Num int, Name varchar(2), Time DATETIME)
insert into @tab select 1 ,'a', '2009/05/01'
insert into @tab select 1 ,'a', '2009/05/02'
insert into @tab select 1 ,'a', '2009/05/03'
insert into @tab select 2 ,'b', '2009/05/04'
insert into @tab select 2 ,'b', '2009/05/05'
insert into @tab select 3 ,'c', '2009/05/06'
insert into @tab select 3 ,'c', '2009/05/07'
insert into @tab select 5 ,'e', '2009/05/08'
insert into @tab select 1 ,'a', '2009/05/09'
insert into @tab select 1 ,'a', '2009/05/10'
select * from @Tab t where not exists(select 1 from @Tab where num=t.num and [time]<t.[time])
/*
Num Name Time
----------- ---- -----------------------
1 a 2009-05-01 00:00:00.000
2 b 2009-05-04 00:00:00.000
3 c 2009-05-06 00:00:00.000
5 e 2009-05-08 00:00:00.000
(4 行受影响)
*/