
A表记录:客户编号, 消费金额;
B表记录: 客户编号和客户公司编号;
C表记录: 客户公司编号和客户公司名称.
现在要查询每个客户公司名称对应的消费金额的总和.
这个SQL查询要怎么做啊?
每个客户公司下面会对应多个客户编号.
A表中的消费金额总和并不等于要查询公司的消费总和.
拜托了...
客户编号:CusID
消费金额滑举罩:Amt
客信闹户公司编号:CompID
客答尘户公司名称:CompNM
sql语句:
select C.CompNM,sum(A.Amt) from A left join B on A.CusID=B.CusID left join C on C.CompID=B.CompID group by C.CompNM order by C.CompNM
end
select count(金额) from A表 where 客户梁察悔编号=(select 客户编号 from B 表 where 客户公司编号=(select 客橡正户没枣编号 from c表 where 客户公司名称=‘“客户公司名称”’))
select C表.客户公司名称,sum(A表.消费金额) from A表 left jion B表 on A表.客户编号=B表.客户编号
left jion C表知历 on C表.客御茄户公司编号=B表.客搭拆搜户公司编号
select 客户公司消镇名称,sum(消费金额) from A a,B b,C c
where a.客户编码=b.客户编码 and b. 客户公司编兄蔽码 =c.客户公司编码
group by 客户公司拿尘粗名称
肯定要用到2个sum函数
客户编号:cid
消费金额:money
客户公司编号:cno
客户公司名称:cname
select C.cno,sum(s.s1) from C,B,
(select cid,sum(money)as s1 from A group by cid)as s (查询客户的消费总额)
where C.cno=B.cno and B.cid=s.cid
group by C.cno (整个语句就是查启镇仔询公司的消费旅纤总和)悄汪