计算与sql server中同一客户关联的所有值的每一行的百分比

时间:2017-07-13 13:36:16

标签: sql-server

我有一个包含客户数据的表

ID  Name  Product  Quantity
1  Rick  Pen  10
1  Rick  Eraser  13
1  Rick  Ink  23
2  Marge  PostIt  13
2  Marge  Glue  7
3  Frank  Pen  13
3  Frank  Glue  6
3  Frank  Ink  8

我想获得一个输出,对于每个客户,我得到每行的数量基于该特定客户的总数量的百分比。例如,客户1有3个项目,其数量总计为46.因此第一行将是(10/46)* 100,大约为21.74%。这就是输出的样子。

ID  Name  Product  Quantity  Total
1  Rick  Pen  10  21.74
1  Rick  Eraser  13  28.26
1  Rick  Ink  23  50.00
2  Marge  PostIt  13  65.00
2  Marge  Glue  7  35.00
3  Frank  Pen  13  48.15
3  Frank  Glue  6  22.22
3  Frank  Ink  8  29.63

非常感谢任何帮助。感谢。

3 个答案:

答案 0 :(得分:2)

您可以使用和分配,如下所示

Select *, PercentSum = Format((Quantity*1.0)/(sum(Quantity) over(partition by Id)), 'p') 
   from yourtable

输出如下:

+----+-------+---------+----------+------------+
| Id | Name  | Product | Quantity | PercentSum |
+----+-------+---------+----------+------------+
|  1 | Rick  | Pen     |       10 | 21.74 %    |
|  1 | Rick  | Eraser  |       13 | 28.26 %    |
|  1 | Rick  | Ink     |       23 | 50.00 %    |
|  2 | Marge | PostIt  |       13 | 65.00 %    |
|  2 | Marge | Glue    |        7 | 35.00 %    |
|  3 | Frank | Pen     |       13 | 48.15 %    |
|  3 | Frank | Glue    |        6 | 22.22 %    |
|  3 | Frank | Ink     |        8 | 29.63 %    |
+----+-------+---------+----------+------------+

答案 1 :(得分:0)

试试这个给出你期望的结果

;With cte(ID, Name, Product, Quantity)
AS
(
SELECT 1,'Rick' ,'Pen' , 10    UNION ALL
SELECT 1,'Rick' ,'Eraser'  ,13 UNION ALL
SELECT 1,'Rick' ,'Ink' , 23    UNION ALL
SELECT 2,'Marge','PostIt' , 13 UNION ALL
SELECT 2,'Marge','Glue' , 7    UNION ALL
SELECT 3,'Frank','Pen'  ,13    UNION ALL
SELECT 3,'Frank','Glue' , 6    UNION ALL
SELECT 3,'Frank','Ink' , 8
)
SELECT ID
    ,NAME
    ,Product
    ,Quantity
    ,CAST(ROUND(Quantity * 1. / (
                SUM(Quantity) OVER (
                    PARTITION BY ID ORDER BY ID
                    )
                ) * 100, 3) AS DECIMAL(4, 2)) AS [Total%]
FROM cte
ORDER BY 1,5

结果

ID  Name    Product Quantity    Total%
-----------------------------------------
1   Rick    Pen     10          21.74
1   Rick    Eraser  13          28.26
1   Rick    Ink     23          50.00
2   Marge   Glue    7           35.00
2   Marge   PostIt  13          65.00
3   Frank   Glue    6           22.22
3   Frank   Ink     8           29.63
3   Frank   Pen     13          48.15

答案 2 :(得分:0)

如果您仍然在2008年或更早版本,并且在sum函数中不支持分区,则可以执行以下操作。如果您在2012年或之后,我会使用Kannan的答案。

declare @Something table
(
    ID int
    , Name varchar(10)
    , Product varchar(10)
    , Quantity int
)

insert @Something
(
    ID
    , Name
    , Product
    , Quantity
)
Values
(1, 'Rick', 'Pen', 10)
, (1, 'Rick', 'Eraser', 13)
, (1, 'Rick', 'Ink', 23)
, (2, 'Marge', 'PostIt', 13)
, (2, 'Marge', 'Glue', 7)
, (3, 'Frank', 'Pen', 13)
, (3, 'Frank', 'Glue', 6)
, (3, 'Frank', 'Ink', 8)

select s.ID
    , s.Name
    , s.Product
    , s.Quantity
    , TotalPercentage = convert(numeric(5,2), (s.Quantity * 1.0 / total.TotalPurchased) * 100)
from @Something s
cross apply
(
    select sum(s2.Quantity) as TotalPurchased
        , ID
    from @Something s2
    where s2.ID = s.ID
    group by s2.ID
) total