计算付款间隔

时间:2018-12-03 22:33:43

标签: sql sql-server

在寻求countpercent的客户中有一定金额应付款的客户方面,我要求您的协助。我正在使用SQL Server 2016。

select customer_id,payments from mytable 


Customer_ID             Payments
   1                     $50
   2                     $100
   3                     $500
   4                     $550
   5                     $500
   6                     $500
   7                     $500
   8                     $400
   9                     $550
   10                    $400

请求的结果:

 Count              Percent                     Payments
   1                   10%                        $50
   2                   20%                        $400
   4                   40%                        $500
   2                   20%                        $550 
   1                   10%                        $100

2 个答案:

答案 0 :(得分:1)

你可以喜欢

SELECT COUNT(Payments) [Count],
       CAST(COUNT(Payments) * 100 / (SELECT COUNT(*) FROM T) AS VARCHAR) + '%' [Percent],
       Payments
FROM T
GROUP BY Payments;

Demo

答案 1 :(得分:0)

我认为您想要一个窗口函数:

select payment, count(*),
       count(*) * 1.0 / sum(count(*)) over () as ratio
from t
group by payment;