如何分别计算单个类别的列的总和?

How to calculate the sum of a column by individual category separately?

我需要一些帮助

我需要计算每位员工的总小时数,并在同一个 table.

中显示每位员工的所有日期

我的Table如下-

Employee Date hours
Rohit 06-03-2022 9
Rohit 07-03-2022 8
Rohit 08-03-2022 9
Rohit 09-03-2022 9
Rohit 10-03-2022 10
Rohit 11-03-2022 8
Rohit 12-03-2022 8
Rohit 06-03-2022 7
Raj 07-03-2022 8
Raj 08-03-2022 4
Raj 09-03-2022 3
Raj 10-03-2022 5
Raj 11-03-2022 8
Raj 12-03-2022 8

输出应该是这样的

Employee Date hours
Rohit 06-03-2022 9
Rohit 07-03-2022 8
Rohit 08-03-2022 9
Rohit 09-03-2022 9
Rohit 10-03-2022 10
Rohit 11-03-2022 8
Rohit 12-03-2022 8
Rohit 06-03-2022 7
Total 68
Raj 07-03-2022 8
Raj 08-03-2022 4
Raj 09-03-2022 3
Raj 10-03-2022 5
Raj 11-03-2022 8
Raj 12-03-2022 8
Total 36

你可以使用 GROUPING SETS 这个

SELECT
  Employee = CASE WHEN GROUPING(t.Date) = 0 THEN t.Employee ELSE 'Total' END,
  t.Date,
  hours = SUM(t.hours)
FROM YourTable t
GROUP BY GROUPING SETS (
    (Employee, Date),
    (Employee)
);

db<>fiddle

The GROUPING() function tells you whether a column has been aggregated for that row