现在有两个表,一个是存储银行数据的表,另一个商品表;商品表中有一个银行编号的字段与银行表关联。商品表中其他的字段为:商品编号(主键),商品总金额,商品状态(分为已售商品和未售商品);问题是:我现在要查询与银行相关的商品的总数量,商品的总金额,已销售商品的数量,已销售商品的总金额,我写的这段SQL有点问题,请问大侠们能给点建议吗?
select yinhang.mingcheng as yinhangmc,count(*) as zongshul,sum(shangpin.zongjine) as zongjine, (select count(*) from shangpin where shangpinzt='已销售' group by yinhangbh) as yixiaosspsl,(select sum(zongjine) from shangpin where shangpinzt='已销售' group by yinhangbh) where shangpin.yinhangbh=yinhang.bianhao group by shangpin.yinhangbh;
在线等呢,对了就给分!
select yinhang.mingcheng as yinhangmc,count(*) as zongshul,sum(shangpin.zongjine) as zongjine, (select count(*) from shangpin where shangpinzt='已销售' group by yinhangbh) as yixiaosspsl,(select sum(zongjine) from shangpin where shangpinzt='已销售' group by yinhangbh) where shangpin.yinhangbh=yinhang.bianhao group by shangpin.yinhangbh;
在线等呢,对了就给分!
把你的表结构,测试数据贴上来,然后贴目标结果
帮你写SQL
看你的SQL看不懂的
001 中国银行 sp001 001 音响 200.00 已售
002 中国工商银行 sp002 001 水 2.00 未售
003 中国建设银行 sp003 002 烟 15.00 取消中
sp004 003 轿车 10000.00 已售
目标结果是:
银行名称 商品数量 商品总金额 已销售商品数量 已销售的商品总金额
中国银行 2 202.00 1 200
中国工商银行 1 15.00 0 0.00
中国建设银行 1 10000.00 1 10000.00
count(*) 商品数量,
sum(b.zongjine) 商品总金额,
sum(decode(b.shangpinzt,'已销售',1,0)) 已销售商品数量 ,
sum(decode(b.shangpinzt,'已销售',b.zongjine,0)) 已销售的商品总金额
from yinghang a,shangpin b
where b.yinhangbh=a.bianhao
group by a.mingcheng
count(decode(T1.商品状态,'已售',1,0)) 已销售商品数量,
sum(decode(T1.商品状态,'已售',T1.商品金额,0)) 已销售的商品总金额
from 商品表 T1,银行表 T2
where T1.银行编号 = T2.编号
group by T2.银行名称
count(decode(T1.商品状态,'已售',1,0)) 已销售商品数量,
sum(decode(T1.商品状态,'已售',T1.商品金额,0)) 已销售的商品总金额
from 商品表 T1,银行表 T2
where T1.银行编号 = T2.编号
group by T2.银行名称
T2.银行名称,
count(T1.编号) 商品数量,
sum(T1.商品金额) 商品总金额
count(decode(T1.商品状态,'已售',1,0)) 已销售商品数量,
sum(decode(T1.商品状态,'已售',T1.商品金额,0)) 已销售的商品总金额
from 商品表 T1,银行表 T2
where T1.银行编号 = T2.编号
group by T2.银行名称
晕,一刷新怎么提交了这么多次。