学习型 ASP/PHP/ASP.NET 主机 30元/年全能 ASP/PHP/ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付
发新话题
打印

怎么消除重复值

怎么消除重复值

以上三个栏位分别为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

TOP

DISTINCT可以消除重复行

TOP

试一下

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)

TOP

发新话题