SQL:平均完成任务的时间

时间:2019-08-08 15:36:30

标签: sql sql-server tsql

我想根据时间段内给出的任务数量,找出开发人员平均需要多长时间才能完成任务。我以前从未在SQL中使用过AVG()函数,因此有点新手。我拥有执行我认为的功能所需的所有值,但不确定如何编写该语句。

我想我最需要的建议是如何处理它或一些示例。我什么都找不到,不过我敢肯定有一种方法可以完成这项任务。

DECLARE
    @Date AS DATETIME = '2019-08-08 00:00:00.000',
    @Developer_Name AS VARCHAR(100) = 'Jim Bob'
--AS
BEGIN   
    DECLARE 
        --Get User ID
        @UserID AS BIGINT = (SELECT usr.id FROM [dbo].[mantis_user_table] AS usr wHERE usr.realname = @Developer_Name) 

    DECLARE 
        @Resolved_30 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -30, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
        @Resolved_60 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -60, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
        @Resolved_90 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -90, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
        @Assigned_30 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -30, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID),
        @Assigned_60 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -60, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID),
        @Assigned_90 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -90, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID)

    SELECT
        @UserID,
        @Resolved_30,
        @Resolved_60,
        @Resolved_90,
        @Assigned_30,
        @Assigned_90

END

预期结果是平均值的百分比

不确定是否可以正常工作

--Assigned / completed
        SUM(@Assigned_30 / @Resolved_30) AS 'SUM 1',
        SUM(@Assigned_60 / @Resolved_60) AS 'SUM 2',
        SUM(@Assigned_90 / @Resolved_90) AS 'SUM 3'

是平均值还是我如何得出平均值?

更新: 不知道我在这里做错了什么,但是出现了错误:

    --Assigned / completed
    CASE WHEN @Resolved_30 = 0 OR @Assigned_30 = 0 THEN 0 ELSE SUM(@Assigned_30 / @Resolved_30) END AS 'Average Tasks Completed In 30 Days',
    CASE WHEN @Resolved_60 = 0 OR @Assigned_30 = 0 THEN 0 ELSE SUM(@Assigned_60 / @Resolved_60) END AS 'Average Tasks Completed In 60 Days',
    CASE WHEN @Resolved_90 = 0 OR @Assigned_30 = 0 THEN 0 ELSE SUM(@Assigned_90 / @Resolved_90) END AS 'Average Tasks Completed In 90 Days'

信息8134,第16级,州1,第18行 除以零错误。

我知道错误在说什么,但我不确定是什么问题,因为我正在检查0。是说值是NULL吗?

更新: 我认为我的平均计算是错误的:

Resolved_30   Assigned_30  Average Tasks Completed In 30 Days
20                 10                   2

我在这里做错了什么?

更新: 我知道我的数学正确,但是在SQL中无法正确计算。 30应该是90.9%

DECLARE
@Date AS DATETIME = '2019-08-08 00:00:00.000',
@Developer_Name AS VARCHAR(100) = 'Jim Bob'

-AS 开始
    宣布         -获取用户ID         @UserID AS BIGINT =(从[dbo]中选择usr.id。[mantis_user_table]在usr.realname = @Developer_Name处为AS usr

DECLARE 
    @Resolved_30 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -30, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
    @Resolved_60 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -60, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
    @Resolved_90 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -90, @Date) AND @Date AND [status] = 80 AND [reporter_id] = @UserID),
    @Assigned_30 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -30, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID),
    @Assigned_60 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -60, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID),
    @Assigned_90 BIGINT = (SELECT COUNT([id]) FROM [dbo].[mantis_bug_table] bug WHERE [last_updated] BETWEEN DATEADD(DAY, -90, @Date) AND @Date AND [status] = 50 AND [reporter_id] = @UserID)

SELECT
    @UserID AS 'User_ID',
    @Resolved_30 AS 'Resolved_30',
    @Resolved_60 AS 'Resolved_60',
    @Resolved_90 AS 'Resolved_90',
    @Assigned_30 AS 'Assigned_30',
    @Assigned_90 AS 'Assigned_90',
    ((@Resolved_30/@Assigned_30)*100) AS 'Average Tasks Completed In 30 Days',
    SUM((@Resolved_90/@Assigned_90)*100)AS 'Average Tasks Completed In 60 Days',
    SUM((@Resolved_90/@Assigned_90)*100) AS 'Average Tasks Completed In 90 Days'

END

2 个答案:

答案 0 :(得分:0)

在SQL中,您必须考虑不属于传统控制流的集合,例如,这是一种在一个查询中获得所有6个结果的方法:

SELECT COUNT(ID) as Cnt, Staus, (DATEDIFF(DAY,DAY,@Date) % 30) AS PERIOD
FROM [dbo].[mantis_bug_table] bug 
WHERE [status] = 80 AND [reporter_id] = @UserID) AND [last_updated] BETWEEN DATEADD(DAY, -90, @Date) AND @Date
GROUP BY (DATEDIFF(DAY,DAY,@Date) % 30), Status

至于出现错误的那一行我不知道您在做什么-您那里没有列名,所以对我来说毫无意义。我希望这样的事情

SUM(CASE WHEN @Resolved_30 = 0 OR @Assigned_30 = 0 
         THEN 0 
         ELSE column_you_want_to_average END) / total_value AS 'Average'

答案 1 :(得分:0)

这是我解决问题的方式。不是最好的方法,但是有效:我仍然不确定我是否能正确获得平均值,但没有错误:

DELETE FROM users_vip WHERE NOW() > expires
UPDATE users SET rank = "4" WHERE user_id = users_vip.user_id

END

我尝试使用CASE,但是一直给我这个错误。