如何将查询结果分组到同一行?

时间:2018-05-20 18:47:15

标签: sql-server

我有以下查询:

SELECT
    Employee,
    Departmentname,
    [Week],
    Activity,
    Monday    = (Select CASE WHEN datename(dw, WorkDate) = 'MONDAY'    THEN CONVERT(VARCHAR(5), TIME, 108) END),
    Tuesday   = (Select CASE WHEN datename(dw, WorkDate) = 'TUESDAY'   THEN CONVERT(VARCHAR(5), TIME, 108) END),
    Wednesday = (Select CASE WHEN datename(dw, WorkDate) = 'WEDNESDAY' THEN CONVERT(VARCHAR(5), TIME, 108) END),
    Thursday  = (Select CASE WHEN datename(dw, WorkDate) = 'THURSDAY'  THEN CONVERT(VARCHAR(5), TIME, 108) END),
    Friday    = (Select CASE WHEN datename(dw, WorkDate) = 'FRIDAY'    THEN CONVERT(VARCHAR(5), TIME, 108) END),
    Saturday  = (Select CASE WHEN datename(dw, WorkDate) = 'SATURDAY'  THEN CONVERT(VARCHAR(5), TIME, 108) END),
    substring(cast(convert(Time, dateadd(millisecond, sum(datediff(millisecond, 0, cast([Time] as datetime))), 0), 108) as VARCHAR(5)),0,9) AS 'TotalHRW'
FROM
    TimeSheet
WHERE
    [Week] LIKE 18
    AND
    Departmentname LIKE 'Returns'
    AND
    Employee LIKE 'Hand Batis'
GROUP BY
    Activity,
    WorkDate,
    Employee,
    Departmentname, 
    [Week],
    Time
ORDER BY
    Week,
    Employee

产生以下结果:

enter image description here

现在,我的问题是,如何修改此查询以在同一行输出所有活动结果?例如,我不希望在给定的一天每次显示一个新行,而是希望只有一个活动名称,并且在每个特定日期输入同一行中的所有时间。

1 个答案:

答案 0 :(得分:1)

将其作为子查询包装在一个查询中,该查询按除WorkDate之外的相同字段进行分组,并为每个工作日获取非NULL值:

SELECT
    Employee,
    DepartmentName,
    Week,
    Activity,
    MIN( Monday ) AS Monday,
    MIN( Tuesday ) AS Tuesday ,
    MIN( Wednesday ) AS Wednesday ,
    MIN( Thursday ) AS Thursday ,
    MIN( Friday ) AS Friday ,
    MIN( Saturday ) AS Saturday
FROM
(
    -- original query here:
    SELECT
        Employee,
        Departmentname,
        [Week],
        Activity,
        Monday    = (Select CASE WHEN datename(dw, WorkDate) = 'MONDAY'    THEN CONVERT(VARCHAR(5), TIME, 108) END),
        Tuesday   = (Select CASE WHEN datename(dw, WorkDate) = 'TUESDAY'   THEN CONVERT(VARCHAR(5), TIME, 108) END),
        Wednesday = (Select CASE WHEN datename(dw, WorkDate) = 'WEDNESDAY' THEN CONVERT(VARCHAR(5), TIME, 108) END),
        Thursday  = (Select CASE WHEN datename(dw, WorkDate) = 'THURSDAY'  THEN CONVERT(VARCHAR(5), TIME, 108) END),
        Friday    = (Select CASE WHEN datename(dw, WorkDate) = 'FRIDAY'    THEN CONVERT(VARCHAR(5), TIME, 108) END),
        Saturday  = (Select CASE WHEN datename(dw, WorkDate) = 'SATURDAY'  THEN CONVERT(VARCHAR(5), TIME, 108) END),
        substring(cast(convert(Time, dateadd(millisecond, sum(datediff(millisecond, 0, cast([Time] as datetime))), 0), 108) as VARCHAR(5)),0,9) AS 'TotalHRW'
    FROM
        TimeSheet
    WHERE
        [Week] LIKE 18
        AND
        Departmentname LIKE 'Returns'
        AND
        Employee LIKE 'Hand Batis'
    GROUP BY
        Activity,
        WorkDate,
        Employee,
        Departmentname, 
        [Week],
        Time
    ORDER BY
        Week,
        Employee
) AS originalQuery
GROUP BY
    Employee,
    DepartmentName,
    Week,
    Activity

...虽然您丢失了TotalHRW列。

另一种方法可能是从原始表格数据中执行UNPIVOT,但您尚未发布表格架构。