sql 最后一行 合计 60
怎么让合计到最后一行啊createtabledingdan(dingdanhaoint,shangpinmingvarchar(20)notnull,shuliangva...
怎么让合计到最后一行啊
create table dingdan(dingdanhao int ,shangpinming varchar(20) not null,shuliang varchar(20) not null,shoujia money not null)
select * from dingdan
insert into dingdan(dingdanhao,shangpinming,shuliang,shoujia)select 88880001,'1',3,45 unionselect 88880001,'王老吉',4,130 unionselect 88880001,'六个核桃',5,270 unionselect 88880001,'统一',6,450 unionselect 88880001,'康师傅',7,528 unionselect 88880001,'达利园',8,320
select shangpinming as 商品名,shuliang as 数量 ,shoujia as 金额 from dingdan unionselect '','合计金额',sum(shuliang*shoujia) from dingdan 展开
create table dingdan(dingdanhao int ,shangpinming varchar(20) not null,shuliang varchar(20) not null,shoujia money not null)
select * from dingdan
insert into dingdan(dingdanhao,shangpinming,shuliang,shoujia)select 88880001,'1',3,45 unionselect 88880001,'王老吉',4,130 unionselect 88880001,'六个核桃',5,270 unionselect 88880001,'统一',6,450 unionselect 88880001,'康师傅',7,528 unionselect 88880001,'达利园',8,320
select shangpinming as 商品名,shuliang as 数量 ,shoujia as 金额 from dingdan unionselect '','合计金额',sum(shuliang*shoujia) from dingdan 展开
2个回答
展开全部
select shangpinming as 商品名,shuliang as 数量 ,shoujia as 金额 from dingdan
union
select '','合计金额',sum(shuliang),sum(shoujia) from dingdan
我看家你的金额已经是每个品种的合计了,这里就不用乘了。
如果你是想直接出来的话(从你计算单品种的合计开始),我记得group by 有个rollup还是cube的参数,可以达到你的要求。
union
select '','合计金额',sum(shuliang),sum(shoujia) from dingdan
我看家你的金额已经是每个品种的合计了,这里就不用乘了。
如果你是想直接出来的话(从你计算单品种的合计开始),我记得group by 有个rollup还是cube的参数,可以达到你的要求。
追问
我随意谢的价格 不是合计 依然需要乘 group by 什么的还没学 不知道咋用
追答
这个你去网上查查吧,不过group by可以实现你的要求,还需要用到rollup或者cube,如果你的是oracle的话,如果是其他数据库,我没用过,不知道一不一样,不过大概方向应该差不多。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询