如何从Group By Select计算百分比

时间:2017-04-28 09:08:21

标签: sql sql-server

我有一个包含此列的生产表:

PRODUCTION_DATE  EMPLOYEE  ITEM  ITEM_QTY
-----------------------------------------
1/1/2000           ANA      A       5
1/1/2000           ANA      B       8
1/1/2000          CAROL     A       3
1/1/2000          CAROL     B       9
1/2/2000           ANA      A       4
1/2/2000           ANA      B       8
1/2/2000          CAROL     A       5
1/2/2000          CAROL     B       7

然后我每周用这段代码对这些数据进行分组:

SELECT      DATEPART(WK,[PRODUCTION_DATE]) AS [WEEK_PROD],
            [EMPLOYEE],
            [ITEM],
            SUM([ITEM_QTY]) AS [TOTAL_QTY],
FROM        PRODUCTION_TABLE

GROUP BY    YEAR([PRODUCTION_DATE]),
            DATEPART(WK,[PRODUCTION_DATE]),
            [EMPLOYEE],
            [ITEM]

使用上面的代码,我可以显示如下数据:

WEEK_PROD  EMPLOYEE  ITEM  TOTAL_QTY
-------------------------------------
    1        ANA       A       9
    1        ANA       B       16
    1       CAROL      A       8
    1       CAROL      B       16

问题是,我想按员工显示每个项目的百分比:

WEEK_PROD  EMPLOYEE  ITEM  TOTAL_QTY  PERCENTAGE
------------------------------------------------
    1        ANA       A       9          36
    1        ANA       B       16         64
    1       CAROL      A       8         33.33
    1       CAROL      B       16        66.67

我该怎么做?

提前致谢

4 个答案:

答案 0 :(得分:1)

窗口函数可以在这里提供帮助,在我看来,它比查询表达式更自然。

SELECT      DISTINCT DATEPART(WK,[PRODUCTION_DATE]) AS [WEEK_PROD],
            [EMPLOYEE],
            [ITEM],
            SUM([ITEM_QTY]) OVER(PARTITION BY EMPLOYEE, ITEM) AS [TOTAL_QTY],
            SUM([ITEM_QTY]) OVER(PARTITION BY EMPLOYEE, ITEM) * 1.0 / SUM([ITEM_QTY]) OVER(PARTITION BY EMPLOYEE) AS [PERCENTAGE]
FROM        dbo.PRODUCTION_TABLE

答案 1 :(得分:0)

在JOIN语句中使用子查询:

SELECT [EMPLOYEE],[ITEM],SUM([ITEM_QTY]) AS [TOTAL_QTY],
       (SUM([ITEM_QTY]) * 100) / Total
FROM #Table
JOIN 
(
   SELECT [EMPLOYEE] _EMPLOYEE ,SUM([ITEM_QTY]) Total
   FROM #Table
   GROUP BY [EMPLOYEE]
) A ON EMPLOYEE = _EMPLOYEE
GROUP BY [EMPLOYEE],[ITEM],Total

答案 2 :(得分:0)

尝试此查询,

;WITH [CTE_EMP]
AS
(
SELECT      DATEPART(WK,[PRODUCTION_DATE]) AS [WEEK_PROD],
            [EMPLOYEE],
            [ITEM],
            SUM([ITEM_QTY]) AS [TOTAL_QTY]
FROM        @PRODUCTION_TABLE
GROUP BY    YEAR([PRODUCTION_DATE]),
            DATEPART(WK,[PRODUCTION_DATE]),
            [EMPLOYEE],
            [ITEM]
)

SELECT *, ([TOTAL_QTY] * 100) / SUM([TOTAL_QTY]) OVER (PARTITION BY [EMPLOYEE],[WEEK_PROD]) AS [PERCENTAGE]
FROM [CTE_EMP]

答案 3 :(得分:0)

这是一个只向您的内部添加超级查询的查询(未更改,内部部分):

SELECT usage_history.*
FROM usage_history
WHERE (site = your_site)
ORDER BY usage_history.timestamp_column DESC
LIMIT $1

输出:

SELECT A.*
, (TOTAL_QTY+0.00)/SUM(TOTAL_QTY) OVER (PARTITION BY EMPLOYEE) *100  AS PERC 
FROM 
    (SELECT      DATEPART(WK,[PRODUCTION_DATE]) AS [WEEK_PROD],
                [EMPLOYEE],
                [ITEM],
                SUM([ITEM_QTY]) AS [TOTAL_QTY]          
    FROM        mytable
    GROUP BY    YEAR([PRODUCTION_DATE]),
                DATEPART(WK,[PRODUCTION_DATE]),
                [EMPLOYEE],
                [ITEM]
            ) A