select min(ID), min(time), 卡号 from table1 group by 卡号
5
有图有真相
图中是sql server优化过的,原始本人是这么写的
select a.ID, a.time, a.卡号
from Table1 as a,(select min(ID) as ID, 卡号 from Table1 group by 卡号) as b
where a.卡号 = b.卡号 and a.ID = b.ID
order by a.ID
5
Select
t1.Id, t1.Time, t1.卡号
from
Table1 t1
Left Join (select Min(Id) From Table1 group by 卡号) t2 on t1.Id = t2.Id
where
t2.Id Is Not Null
5
假如只是过滤重复 ID,就如 #21 楼那样的例子,写为
select tb.ID, tb.time, tb.[卡号] from (select min(ID) as ID from [your table] group by [卡号]) as ta
left join [your table] as tb
on ta.ID=tb.ID
这样的关系运算即可。SQL Server 会自动将“In”运算改为关系运算符,所以你假如了解普通的关系运算(left join、inner join)那么对 sql 的知识就更加完备。
假如要取每一个“卡号”第一次发生的准确时间,可以这样写
select ta.ID, ta.time, ta.[卡号] from (select 卡号,min(time) as time from [your table] group by 卡号 ) as tb
left join [your table] as ta
on ta.卡号=tb.卡号 and ta.time=tb.time
第二个跟第一个其实非常相似。会了第一个,就很容易理解第二个查询。
5
select first( ID),first(TIme),卡号 from table1 group by 卡号.但是这种语法只能access里面用。
其他的本人也不知道怎么提取第一行的数据。
本人只是想数据去重;要提取数据吗,作为dataGridView1的DataSource ;
假如只是过滤重复 ID,就如 #21 楼那样的例子,写为
select tb.ID, tb.time, tb.[卡号] from (select min(ID) as ID from [your table] group by [卡号]) as ta
left join [your table] as tb
on ta.ID=tb.ID
这样的关系运算即可。SQL Server 会自动将“In”运算改为关系运算符,所以你假如了解普通的关系运算(left join、inner join)那么对 sql 的知识就更加完备。
假如要取每一个“卡号”第一次发生的准确时间,可以这样写
select ta.ID, ta.time, ta.[卡号] from (select 卡号,min(time) as time from [your table] group by 卡号 ) as tb
left join [your table] as ta
on ta.卡号=tb.卡号 and ta.time=tb.time
第二个跟第一个其实非常相似。会了第一个,就很容易理解第二个查询。
太无耻了居然抄本人的。本人要告老师!
5
第二个查询是第一个查询的扩展,也就是说,在进行关心运算时,不仅仅要求 ID 匹配而且要求 time 匹配,才是结果。这能得到准确的结果。
假如是 select min(time),min(ID) 那就会得到跟原始数据不搭界的查询结果了。
select id,time,[卡号] from (select *,ROW_NUMBER() over (partition by [卡号] order by [time] desc) as rw
from [table1] ) as t1 where rw = 1 and id =2
用这个会有问题吗?
select id,time,[卡号] from (select *,ROW_NUMBER() over (partition by [卡号] order by [time] desc) as rw
from [table1] ) as t1 where rw = 1 and id =2
这个是oracle的写法,假如是sqlserver,还是用#26这样写吧
select ta.ID, ta.time, ta.[卡号] from (select 卡号,min(time) as time from [your table] group by 卡号 ) as tb
left join [your table] as ta
on ta.卡号=tb.卡号 and ta.time=tb.time