作者pttsasho (sasho)
看板Database
标题[SQL ] 如何暂存查询结果作为後续的参数使用
时间Sun Sep 15 01:11:33 2013
假设有以下Table
Customer Amount
-----------------------
A 10
B 20
C 30
如果我想计算A、B、C三个客户
分别占本公司销售比重的话,必须先计算出总销售数量 (60)
然後再分别将个别客户的销售数量除以总数量
想请教各位前辈,是否有办法利用SQL语法一次完成呢?
感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 118.171.210.3
1F:→ fantasyj:select sum(amount) as a, avg(amount) as b from table 09/15 08:54
2F:→ pttsasho:这样求出来的应该是平均值吧! 我想查询的是各别客户的 09/15 09:26
3F:→ pttsasho:比重。比方说A客户比重为:10/60 = 0.167 这样,谢谢! 09/15 09:27
4F:→ fantasyj:select a.customer, a.amount, a.amount / b.toal_amt 09/15 18:33
5F:→ fantasyj: from table as a, 09/15 18:34
6F:→ fantasyj: (select sum(b.amount) as total_amt 09/15 18:34
7F:→ fantasyj: from table as b) as b 09/15 18:34