0

我有一个问题表,用户可以在其中记录工作时间并估计看起来像这样的时间

id |  assignee  |  task   |   timespent  |  original_estimate | date
--------------------------------------------------------------------------
1  |  john      |  design |   2          |  3                 | 2013-01-01
2  |  john      |  mockup |   2          |  3                 | 2013-01-02
3  |  john      |  design |   2          |  3                 | 2013-01-01
4  |  rick      |  mockup |   5          |  4                 | 2013-01-04

我需要按任务和日期对工作时间和估计时间进行汇总和分组才能得到这个

assignee  |  task   | total_spent   | total_estimate  |  date
------------------------------------------------------------------
john      |  design | 4             | 6               | 2013-01-01
john      |  mockup | 2             | 3               | 2013-01-02
rick      |  design | 5             | 4               | 2013-01-04

好的,这很容易,我已经有了这个:

SELECT assignee, task, SUM(timespent) as total_spent, SUM(original_estimate) AS total_estimate, date FROM issues GROUP BY assignee, task, date

我的问题是我还需要显示当天没有记录任何任务时间的受让人,我的意思是:

assignee  |  task   | total_spent   | total_estimate  |  date
------------------------------------------------------------------
john      |  design | 4             | 6               | 2013-01-01
john      |  mockup | 2             | 3               | 2013-01-02
rick      |  design | 5             | 4               | 2013-01-04
pete      |  design | 0             | 0               | 2013-01-01
pete      |  mockup | 0             | 0               | 2013-01-02
liz       |  design | 0             | 0               | 2013-01-04
liz       |  mockup | 0             | 0               | 2013-01-04

目标是绘制这样的图表http://jsfiddle.net/uUjst/embedded/result/

4

3 回答 3

1

您需要在他们自己的单独表中加入受让人。

SELECT  tblAssignee.Name, task, SUM(timespent) as total_spent, SUM(original_estimate) AS   total_estimate, date 
FROM tblAssignee
    LEFT JOIN issue ON issues.assignee = tblAssignee.Name
GROUP BY tblAssignee.Name, task, date
于 2013-01-23T18:33:07.160 回答
0

假设您有一个user表,但没有一个tasksordates表......这意味着我们必须从存在的值中得出这些值issues

;WITH dates AS (
    SELECT DISTINCT date
    FROM issues
), tasks AS (
    SELECT DISTINCT task
    FROM issues
)
SELECT 
    u.user as assignee, 
    t.task, 
    SUM(i.timespent) as total_spent, 
    SUM(i.original_estimate) AS total_estimate, 
    d.date 
FROM 
    users u CROSS JOIN
    dates d CROSS JOIN
    tasks t LEFT OUTER JOIN
    issues i ON
        i.assignee = u.user
        AND i.task = t.task
        AND i.date = d.date
GROUP BY u.user, t.task, d.date
于 2013-01-23T18:34:50.150 回答
0
SELECT
   A.name,
   task,
    ISNULL(SUM(timespent), 0) as total_spent,
    ISNULL(SUM(original_estimate), 0) AS total_estimate,
    date
FROM Assignee A
    LEFT JOIN issue
        ON issues.assignee = A.Name
GROUP BY A.name, task, date
于 2013-01-23T18:39:14.453 回答