declare @t table (item varchar(20),itemcode varchar(20))
insert into @t (item,itemcode)values ('choco','ckschoc'),('chocowafer','wfrchoc'),('chocostrrae','wfrstr')
declare @tt table (ordcnt int,itemcode varchar(20),dated date)
insert into @tt (ordcnt,itemcode,dated)values (20,'ckschoc','4/24/2015'),(10,'wfrchoc','4/24/2015'),(15,'wfrstr','4/24/2015')
,(30,'ckschoc','4/24/2015'),(20,'wfrstr','4/24/2015')
we can achieve the same result using sub query and corelated join query also
解决方案
select t.itemcode,p.S from @t t INNER JOIN (
select itemcode,SUM(ordcnt)S from @tt
GROUP BY itemcode)P
ON p.itemcode = t.itemcode
group by t.itemcode,P.s
select t.itemcode,
(select SUM(tt.ordcnt)Cnt from @tt tt
where tt.itemcode = t.itemcode
group by tt.itemcode )Cnt from @t t