我正在使用sybase数据库,我的表的提取将是:
ClientNumber|ArticlesBought|TotalAllowed
2223 |2 |20
2223 |1 |20
2226 |3 |25
2226 |2 |25
2227 |1 |20
我需要的是计算所有文章的总和并将其除以不同客户所允许的总和之和。
sum(ArticlesBought) = 9 and sum(TotalAllowed)= 65 and not 110
我的第一个问题是我可以在一个查询中执行此操作吗? 我尝试使用:
select sum(TotalAllowed)
from myTable
group by ClientNumber
但它返回40,45和20,这是错误的。 请你帮助我好吗 ?
谢谢
答案 0 :(得分:1)
Select my.ClientNumber , my2.TotalArticlesBought/ClientData.TotalAllowedperClient from tblFabSource my
inner join (select sum(Distinct TotalAllowed)as TotalAllowedperClient,ClientNumber from myTable group by ClientNumber)
ClientData on ClientData .ClientNumber=my.ClientNumber
inner join (select sum(ArticlesBought)as TotalArticlesBought from myTable ) my2
on my2.TotalArticlesBought>0
答案 1 :(得分:0)
select SUM(articlesbought), SUM(DISTINCT totalallowed) from tablename
你可以在totalallowed
的总和上使用distinct