日期:2014-05-17 浏览次数:20488 次
CREATE TABLE tb3(
[公司] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[产品] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NULL,
[CreateTime] [datetime] NULL,
[id] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
insert into tb3
select 'a司', '苹果', '2013-1-12'
union all
select 'a司' , '香蕉', '2013-1-12'
union all
select 'a司' , '苹果', '2013-1-10'
union all
select 'a司' , '香蕉', '2013-1-10'
union all
select 'b司' , '苹果', '2013-1-10'
union all
select 'b司' , '香蕉', '2013-1-10'
----删除后
delete from tb3
where id not in
(
select id from
(
select 公司,产品,MAX(CreateTime)time from tb3
group by 公司,产品
)a
left join
(
select * from tb3
)b
on a.公司=b.公司
and a.产品=b.产品
and a.time=b.CreateTime
)
----查询
select 公司,产品,MAX(CreateTime)
from tb3
group by 公司,产品
order by 公司
CREATE TABLE tb
([公司] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[产品] [nvarchar](10) COLLATE Chinese_PRC_CI_AS NULL,
[CreateTime] [date] NULL,
[id] [int] IDENTITY(1,1) NOT NULL
) ON [PRIMARY]
insert into tb
select 'a司',&nbs