标题:怎么消除重复值
只看楼主
sky_yang_sky
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:481
专家分:16
注 册:2007-5-30
结帖率:50%
 问题点数:0 回复次数:2 
怎么消除重复值
以上三个栏位分别为fdate,fscstockid,fitemid,现在要实现如果fscstockid,fitemid两个栏位的数据没有重复,就取出来,如果是重复的,就取时间最近的一笔,请问怎么实现?
2008-02-20 00:00:00.000    130    512
2008-01-20 00:00:00.000    130    568
2008-01-01 00:00:00.000    132    1040
2008-01-07 00:00:00.000    132    1040
2008-01-09 00:00:00.000    132    1040
2008-01-10 00:00:00.000    132    1040
2008-01-07 00:00:00.000    132    1042
2008-01-09 00:00:00.000    132    1042
2008-01-10 00:00:00.000    132    1042
要成为如下
2008-02-20 00:00:00.000    130    512
2008-01-20 00:00:00.000    130    568
2008-01-10 00:00:00.000    132    1040
2008-01-10 00:00:00.000    132    1042
搜索更多相关主题的帖子: 消除 fitemid fscstockid 栏位 fdate 
2008-04-25 14:24
shezhenhui1989
Rank: 1
来 自:安徽
等 级:新手上路
帖 子:287
专家分:0
注 册:2008-2-1
得分:0 
DISTINCT可以消除重复行
2008-04-25 14:44
lff642
Rank: 1
等 级:新手上路
帖 子:91
专家分:0
注 册:2007-11-28
得分:0 
试一下
create table tb(fdate datetime,fscstockid int,fitemid int)
insert into tb select '2008-02-20 00:00:00.000',130,512
union all select '2008-01-20 00:00:00.000',130,568
union all select '2008-01-01 00:00:00.000',132,1040
union all select '2008-01-07 00:00:00.000',132,1040
union all select '2008-01-09 00:00:00.000',132,1040
union all select '2008-01-10 00:00:00.000',132,1040
union all select '2008-01-07 00:00:00.000',132,1042
union all select '2008-01-09 00:00:00.000',132,1042
union all select '2008-01-10 00:00:00.000',132,1042

select * from tb

select A.* from tb A where not exists(select 1 from tb where fscstockid=A.fscstockid and fitemid=A.fitemid and fdate > A.fdate)
2008-04-25 14:48



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-211030-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.066560 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved