日期:2014-05-17 浏览次数:20486 次
if object_id('[TB]') is not null drop table [TB]
go
create table [TB] (PROD nvarchar(6),PRODQTY int,OPR nvarchar(4),QTY int,DATE datetime)
insert into [TB]
select 'A01',100,'A',5,'2012-01-01' union all
select 'A01',100,'B',25,'2012-01-02' union all
select 'A01',100,'C',35,'2012-01-03' union all
select 'A01',100,'D',15,'2012-01-04' union all
select 'A01',100,'E',15,'2012-01-05' union all
select 'A01',100,'f',10,'2012-01-06' union all
select 'A01',100,'g',12,'2012-01-07' union all
select 'A02',50,'Aa',5,'2012-02-01' union all
select 'A02',50,'Ba',10,'2012-02-02' union all
select 'A02',50,'Ca',20,'2012-02-03' union all
select 'A02',50,'Da',16,'2012-02-04' union all
select 'A02',50,'Ea',15,'2012-02-05' union all
select 'A02',50,'fa',10,'2012-02-06' union all
select 'A02',50,'ga',12,'2012-02-07'
select * from [TB]
SELECT TB.*
FROM TB
INNER JOIN ( SELECT TOP 1
A.date ,
A.prodqty ,
A.PROD ,
SUM(B.QTY) AS qty
FROM dbo.TB A