实现这个功能sql语句不会写

.Net技术 码拜 8年前 (2016-09-16) 1231次浏览
vs2010(C#),sql2012
表:table1
ID          time       卡号   分数
1        2016-7      01       65
2        2016-8      01       70
3        2016-5      03       66
4        2016-3      02       75
本人想得到的是卡号相同只显示一个,分数那一列不显示,如下:
ID          time       卡号
1        2016-7      01
3        2016-5      03
4        2016-3      02
怎么实现:select  ?distinct?  group by? 不知道怎么写?
解决方案

5

select id,time,[卡号] from (
select *,ROW_NUMBER() over (partition by [卡号] order by [time] desc)  as rw
from [table1] ) as t1
where rw = 1;

参考资料:SQLServer数据去重问题

5

select min(ID),  min(time),  卡号 from table1 group by 卡号

5

有图有真相
实现这个功能sql语句不会写
图中是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

引用:
Quote: 引用:

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

引用:
Quote: 引用:
Quote: 引用:

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

引用:
Quote: 引用:

第二个查询是第一个查询的扩展,也就是说,在进行关心运算时,不仅仅要求 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

5

引用:

你的ID=2不见了,
本人的数据是多台下位机发送接收的,每台下位机有一个ID号,本人是想查询这个ID的数据但查询里卡号重的只显示一条,查询的只显示 ID、time、卡号这三列;

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

本人用着这个,如下结果:
表:table1
ID          time       卡号   分数
1        2016-7      05       65
2        2016-8      01       70
2        2016-6      01       78
2        2016-5      03       66
4        2016-3      02       75
查询结果如下:
ID          time       卡号
2        2016-8      01
2        2016-5      03

看看你本人的问题中是怎么写的。
这个问题没有什么回的必要了。假如表达 DB 设计是乱的,那么结果就不用看了。


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明实现这个功能sql语句不会写
喜欢 (0)
[1034331897@qq.com]
分享 (0)