# sum 求和有关问题,再次求解(2)

www.myexceptions.net  网友分享于：2013-02-12  浏览：12次

请帮忙写出语句   谢谢!

------解决方案--------------------
select goodsID , goodsNAME , sum(goodsSL) , sum(goodsJE ) , goodsGG
from jhview group by goodsID
------解决方案--------------------
select
goodsID,
goodsNAME,
sum(goodsSL),
sum(goodsJE ),
goodsGG
from jhview
group by goodsID

------解决方案--------------------
--改下
select
goodsID,
goodsNAME,
sum(goodsSL),
sum(goodsJE ),
goodsGG
from jhview
group by goodsID,goodsNAME,goodsGG
------解决方案--------------------
create table #jhview (goodsID int,goodsNAME varchar(100),goodsSL int,goodsDJ int,goodsJE int,goodsGG varchar(100),riqi datetime)

insert into #jhview select 1, '钢笔 ',20,2,40, '长城牌 ', '2004-12-15 '
insert into #jhview select 1, '钢笔 ',20,2,40, '长城牌 ', '2005-12-05 '
insert into #jhview select 2, '笔记本 ',30,1,30, '大号 ', '2005-12-15 '
insert into #jhview select 3, '铅笔 ',20,1,20, '长细 ', '2005-12-20 '
insert into #jhview select 4, '钢笔 ',10,4,40, '英雄牌 ', '2005-12-25 '

select
goodsID,
goodsNAME,
sum(goodsSL) as goodsSL,
sum(goodsJE ) as goodsJE,
goodsGG
from #jhview
group by goodsID,goodsNAME,goodsGG

drop table #jhview
------解决方案--------------------

select goodsID , goodsNAME , sum(goodsSL) , sum(goodsJE ) , goodsGG
from jhview group by goodsID,goodsNAME,goodsGG 应该这样 ,不好意思啊

------解决方案--------------------
--这种写法有新意哟，既然楼主开了贴，多写个方案讨论一下。
select goodsID , min(goodsNAME),sum(goodsSL), sum(goodsJE ) ,min(goodsGG)
from jhview group by goodsID
------解决方案--------------------
select goodsID,goodsName,goodsSL=sum(goodsSL),goodsJE=sum(goodsJE),goodsGG from jhview group by goodsID,goodsName,goodsGG
------解决方案--------------------
select goodsID , goodsNAME , sum(goodsSL) , sum(goodsJE ) , goodsGG
from jhview group by goodsID
------解决方案--------------------
select a.goodsID,b.goodsName,a.sum(goodsSl),a.sum(goodsJE),b.goodsGG from (select goodsID,sum(goodsSl),sum(goodsJE) from jhview ) a left join jhview b on a.goodsID=b.goodsID
------解决方案--------------------
select goodsID , goodsNAME , sum(goodsSL) , sum(goodsJE ) , goodsGG
from jhview group by goodsID

------解决方案--------------------
select
goodsID,
goodsNAME,
sum(goodsSL),
sum(goodsJE ),
goodsGG
from jhview
group by goodsID,goodsNAME,goodsGG