2

我希望根据其他两列计算一列,但我有一些特殊情况,计算必须考虑多行。

这是现在的查询:

SELECT
    UserName
    , EntryDate
    , Project
    , HoursWorked
    , HoursAvailable
    , UtilPct as HoursWorked / HoursAvailable
FROM
    MyDatabase
ORDER BY
    EntryDate

结果是:

UserName EntryDate Project HoursWorked HoursAvailable UtilPct
Justin 12/17/2012 ABC 8 8 100
Justin 12/18/2012 ABC 4 8 50
Justin 12/18/2012 DEF 4 8 50

但是最后两个条目的 50% 利用率是错误的,因为两者都发生在同一天。这两行都应该显示 100%。

我需要查询...

  1. 总结某一天的所有工作时间
  2. 除以当天的最大可用小时数
  3. 将该值放入每行的 UtilPct 列

我如何让它做到这一点?

4

4 回答 4

2

我需要查询...

总结某一天的所有 HoursWorked 除以该天的最大 HoursAvailable 将该值放入每行的 UtilPct 列

我如何让它做到这一点?

总结所有的工作时间:

SUM(HoursWorked) ... GROUP BY EntryDate

除以当天的最大可用小时数

SUM(HoursWorked)/MAX(HoursAvailable) ... GROUP BY EntryDate

将该值放入每行的 UtilPct 列中:

SELECT ... SUM(HoursWorked)/MAX(HoursAvailable)*100.0 AS UtilPct
    FROM MyDatabase
    GROUP BY EntryDate
    ORDER BY EntryDate

但是现在您将无法将行除以Project,您只会看到任何一天的时间利用率。如果您尝试这样做GROUP BY Project,您将再次获得 50% 而不是 100%。

那是因为您要求 4/8 为 100%,而这会得到语义上无意义的答案,例如“周二,我在 ABC 项目上 100% 工作,在 DEF 项目上100% 工作”,其中一个可以回答“所以周二有 200% 的人?”

JOIN您可以使用表和自身之间的a 来解决这个难题,以便TotalHoursWorked 在一天内获得,这在语义上与单个项目上的 HoursWorked 不同:

SELECT
    UserName
    , md1.EntryDate
    , Project
    , HoursWorked
    , HoursAvailable
    , (TotalHoursWorked / HoursAvailable)*100.0 AS UtilPct
FROM
    MyDatabase AS md1
    JOIN ( 
        SELECT EntryDate, SUM(HoursWorked) AS TotalHoursWorked
        FROM MyDatabase GROUP BY EntryDate
    ) AS md2 ON (md1.EntryDate = md2.EntryDate)
ORDER BY
    md1.EntryDate

现在 UtilPct 将参考当天的时间使用情况,这意味着即使您只工作了五分钟,您也可以获得 100% 的价值,前提是这五分钟是您当天可用的全部时间。而且您仍然可以添加另一列来指示该项目当天完成的百分比(大多数系统HoursWorked中,它应该与该项目的优先级大致成比例)。HoursAvailable

于 2012-12-17T22:22:05.747 回答
1
SELECT
    a.Username, 
    a.EntryDate,
    a.Project,
    a.HoursWorked,
    (b.TotalHoursWorked / a.HoursAvailable) * 100 as 'UtilPct'
FROM
    MyDatabase a
INNER JOIN
    (SELECT 
       SUM(HoursWorked) as TotalHoursWorked, 
       Username, 
       EntryDate 
     FROM 
       MyDatabase 
     GROUP BY 
       Username, EntryDate) b
ON
  a.Username = b.Username
  AND a.EntryDate = b.EntryDate
于 2012-12-17T22:21:24.500 回答
0

询问:

SQLFIDDLE示例

SELECT
      t.UserName
    , t.EntryDate
    , t.Project
    , t.HoursWorked
    , t.HoursAvailable
    , (SELECT SUM(HoursWorked) 
      FROM MyDatabase 
      WHERE MyDatabase.EntryDate =t.EntryDate)/
      (SELECT MAX(HoursAvailable) 
      FROM MyDatabase 
      WHERE MyDatabase.EntryDate =t.EntryDate) AS UtilPct
FROM
    MyDatabase t
ORDER BY
    t.EntryDate

结果:

| USERNAME |                       ENTRYDATE | PROJECT | HOURSWORKED | HOURSAVAILABLE | UTILPCT |
-------------------------------------------------------------------------------------------------
|   Justin | December, 17 2012 00:00:00+0000 |     ABC |           8 |              8 |       1 |
|   Justin | December, 18 2012 00:00:00+0000 |     ABC |           4 |              8 |       1 |
|   Justin | December, 18 2012 00:00:00+0000 |     DEF |           4 |              8 |       1 |

顺便说一句,表中的好名字。

于 2012-12-17T22:16:01.790 回答
0

子查询只是总结一天。

        SELECT
              t.UserName
            , t.EntryDate
            , t.Project
            , t.HoursWorked
            , t.HoursAvailable
            , CASE WHEN HoursAvailable = 0 THEN 0 
                   ELSE Round(HoursWorked / HoursAvailable * 100.0, 2) 
                   END AS UtilPct
        FROM
            MyTable t
            INNER JOIN 
              (SELECT EntryDate,
                      UserName,          
                      SUM(HoursWorked) AS HoursWorked,
                      MAX(HoursAvailable) AS HoursAvailable
              FROM MyTable
              GROUP BY EntryDate,
                       UserName) AS Daily
        ON t.EntryDate = Daily.EntryDate
           AND t.UserName = Daily.UserName
        ORDER BY
            t.EntryDate,
            t.UserName
于 2012-12-17T22:27:31.933 回答