简体   繁体   English

在T-SQL的销售总额中,销售收入的80%

[英]80% of sales out of Total sales in T-SQL

I have data like this 我有这样的数据

Brand Name  Sales Cost  Mkt
Brand1  3312    ABC
Brand2  3490    ABC
Brand3  7794    ABC
Brand4  8325    ABC
Brand5  3915    ABC
Brand6  6494    ABC

Expected output should be like this: 预期的输出应如下所示:

Brand Name  Sales Cost  Mkt Percentage
Brand1  3312    ABC 10%
Brand2  3490    ABC 17%
Brand3  7794    ABC 80%
Brand4  8325    ABC 89%
Brand5  3915    ABC 12%
Brand6  6494    ABC 59%

I want to find out Brand that holds 80% sales in ABC market? 我想找出在ABC市场上拥有80%销售量的品牌吗? Suggestions please 建议

I guess you want window functions. 我猜你想要窗口功能。 I'm assuming that brandX can occur more than once in your data for one mkt (even though you do not have such rows in your example) 我假设brandX可能出现不止一次在您的数据对于一个mkt (即使你没有在你的例子有这样的行)

select brand, 
       mkt,
       sum(sales), 
       sum(cast(sales as float))/sum(cast(sum(sales) as float)) over (partition by mkt) * 100 
from data
group by brand, mkt

dbfiddle demo dbfiddle演示

and if you want those having more then 80% use subquery and where 如果您希望拥有80%以上的用户使用子查询,

select *
from
(
    select brand, 
           mkt,
           sum(sales) sumsales, 
           sum(cast(sales as float))/sum(cast(sum(sales) as float)) over (partition by mkt) * 100 percentsales
    from data
    group by brand, mkt
) t
where percentsales > 80

Try this: 尝试这个:

select * from (
select [Brand Name],
       [Sales],
       [Cost],
       [Sales]/(sum([Sales]) over (partition by (select null))) [Mkt Percentage]
from MY_TABLE
) [a] where [Mkt Percentage] >= 0.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM