[英]Oracle SQL create rollup totals
我有以下測試數據,我可以使用一些幫助生成以下輸出
YEAR 2021
Week 30
1 Smith,Jane HRS 11 MIN 45 SEC 08
2 Doe,John HRS 07 MIN 01 SEC 11
Week Total HRS 18 MIN 46 SEC 19
Week 31
1 Smith,Jane HRS 13 MIN 36 SEC 01
2 Doe,John HRS 03 MIN 27 SEC 00
Week Total HRS 17 MIN 03 SEC 01
Year Total HRS 35 MIN 49 SEC 20
我想從 emp_attendance 表中提取每個唯一的 YYYY,然后創建 YYYY(年總數)的總數。
在每個 YYYY 中(樣本只有 2 個周數 30,31,但每年最多可以有 52 個)我想創建周數總計。
在每個星期內,我想要每個員工 ID 的總數。
我已經使用以下查詢來獲取小時、分鍾、秒,但我不確定如何匯總數據以在上面的示例輸出中生成我想要的幾個總數。 我更喜歡 ALL sql 解決方案。
select
e.employee_id
e.first_name,
e.last_name,
trunc(sum(a.end_date - a.start_date) * 24) hours,
trunc(mod(sum(a.end_date - a.start_date) * 24 * 60,60)) minutes,
round(mod(sum(a.end_date - a.start_date) * 24 * 60 * 60,60)) seconds
from employees e,
emp_attendance a
where a.employee_id = e.employee_id
group by e.employee_id, e.first_name, e.last_name
order by e.employee_id, e.first_name,
e.last_name;
Emp Fname Lname hrs min sec
1 Jane Smith 25 21 9
2 John Doe 10 28 11
我的測試用例。
ALTER SESSION SET NLS_DATE_FORMAT = 'MMDDYYYY HH24:MI:SS';
Create table employees(
employee_id NUMBER(6),
first_name VARCHAR2(20),
last_name VARCHAR2(20)
);
Insert into employees (employee_id,
first_name,
last_name) values
(1, 'Jane', 'Smith');
Insert into employees (employee_id,
first_name,last_name)
values(2, 'John', 'Doe');
CREATE TABLE emp_attendance(
seq_num integer GENERATED BY DEFAULT AS IDENTITY (START WITH 1) NOT NULL,
employee_id NUMBER(6),
start_date DATE,
end_date DATE,
week_number NUMBER(2),
create_date DATE DEFAULT SYSDATE
);
INSERT INTO emp_attendance (employee_id, start_date, end_date, week_number)VALUES (1, to_date('20210714 18:35:40','YYYYMMDD HH24:MI:SS'), to_date('20210715 02:34:56','YYYYMMDD HH24:MI:SS'),30);
INSERT INTO emp_attendance (employee_id, start_date, end_date, week_number) VALUES (1, to_date('20210715 19:37:10','YYYYMMDD HH24:MI:SS'), to_date('20210715 23:23:02','YYYYMMDD HH24:MI:SS'),30);
INSERT INTO emp_attendance (employee_id, c start_date, end_date, week_number)VALUES (1, to_date('20210722 09:17:11','YYYYMMDD HH24:MI:SS'), to_date('20210722 22:53:12','YYYYMMDD HH24:MI:SS'),31);
INSERT INTO emp_attendance (employee_id, start_date, end_date, week_number)
VALUES (2, to_date('20210716 18:24:11','YYYYMMDD HH24:MI:SS'), to_date('20210717 01:25:22','YYYYMMDD HH24:MI:SS'),30);
INSERT INTO emp_attendance (employee_id, start_date, end_date, week_number)
VALUES (2, to_date('20210722 09:17:11','YYYYMMDD HH24:MI:SS'), to_date('20210722 12:44:11','YYYYMMDD HH24:MI:SS'),31);
GROUPING SETS
子句可用於創建多個聚合,而GROUPING_ID
函數可用於識別每行屬於哪個聚合。
例如:
select
to_char(a.start_date, 'YYYY') the_year,
week_number,
e.employee_id,
e.first_name,
e.last_name,
trunc(sum(a.end_date - a.start_date) * 24) hours,
trunc(mod(sum(a.end_date - a.start_date) * 24 * 60,60)) minutes,
round(mod(sum(a.end_date - a.start_date) * 24 * 60 * 60,60)) seconds,
grouping_id(to_char(a.start_date, 'YYYY'), week_number, e.employee_id, e.first_name, e.last_name) the_grouping_id
from employees e
join emp_attendance a
on a.employee_id = e.employee_id
group by grouping sets
(
(to_char(a.start_date, 'YYYY'), week_number, e.employee_id, e.first_name, e.last_name),
(to_char(a.start_date, 'YYYY'), week_number),
(to_char(a.start_date, 'YYYY')),
()
)
order by week_number, employee_id;
THE_YEAR WEEK_NUMBER EMPLOYEE_ID FIRST_NAME LAST_NAME HOURS MINUTES SECONDS THE_GROUPING_ID
-------- ----------- ----------- ---------- --------- ----- ------- ------- ---------------
2021 30 1 Jane Smith 11 45 8 0
2021 30 2 John Doe 7 1 11 0
2021 30 18 46 19 7
2021 31 1 Jane Smith 13 36 1 0
2021 31 2 John Doe 3 27 0 0
2021 31 17 3 1 7
2021 35 49 20 15
35 49 20 31
這些結果包含您想要的數字,但不是您想要的確切格式。 我假設您將使用報告工具進行格式化,但如果您想純粹在 SQL 中執行此操作,您可以使用復雜的CASE(WHEN THE_GROUPING_ID = 'X' THEN ...
表達式來構建您想要的確切字符串。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.