在 oracle 中创建一个 Pivot SQL
Making a Pivot in oracle SQL
我在 oracle sql 中创建一个枢轴时遇到了一些问题。我有一个 table 和 workdate
,employee
和他们的 shift
一天。这个table叫做SHIFT
。
我希望在此数据透视表中,工作日期成为列标题,员工成为行标题,班次成为日期,这样您就可以看到员工每天的班次。
我当前的问题是我有一个查询,但数据透视表要求我放置一个聚合函数。查询如下:
select employee, workdate, shift from shift
pivot (
max(shift)
for employee
in (
1,2,3,4
)
)
所以我这样做了,它带回了所有列,而不是旋转格式。
当前结构:
employee
workdate
shift
employee1
date1
shift1
employee1
date2
shift2
employee2
date1
shift1
employee2
date2
shift2
employee3
date1
shift1
employee3
date2
shift2
employee4
date1
shift1
employee4
date2
shift2
employee5
date1
shift1
employee5
date2
shift2
我想要的结果是:
date1
date2
date3
employee1
shift1
shift2
shift3
employee2
shift1
shift2
shift3
employee3
shift1
shift2
shift3
employee4
shift1
shift2
shift3
感谢您的回答。
你想要:
select employee,
date1,
date2,
date3
from shift
pivot (
max(shift)
for workdate
in (
DATE '2021-08-17' AS date1,
DATE '2021-08-18' AS date2,
DATE '2021-08-19' AS date3
)
)
其中,对于您的示例数据:
CREATE TABLE shift (employee, workdate, shift) AS
SELECT 'employee1', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee1', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee2', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee2', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee3', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee3', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee4', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee4', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee5', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee5', DATE '2021-08-18', 'shift2' FROM DUAL;
输出:
EMPLOYEE
DATE1
DATE2
DATE3
employee2
shift1
shift2
employee1
shift1
shift2
employee3
shift1
shift2
employee5
shift1
shift2
employee4
shift1
shift2
注意:对于 shift3,您的数据有零行。
db<>fiddle here
如果您需要员工的 where 子句以及工作日期的数据透视表,那么您将编写
SELECT * FROM (
SELECT employee, workdate, shift
FROM shift WHERE employee IN ('employee1', 'employee2', 'employee3')
)
pivot (
max(shift)
for workdate
in (
('date1','date2','date3','date4')
)
)
我在 oracle sql 中创建一个枢轴时遇到了一些问题。我有一个 table 和 workdate
,employee
和他们的 shift
一天。这个table叫做SHIFT
。
我希望在此数据透视表中,工作日期成为列标题,员工成为行标题,班次成为日期,这样您就可以看到员工每天的班次。
我当前的问题是我有一个查询,但数据透视表要求我放置一个聚合函数。查询如下:
select employee, workdate, shift from shift
pivot (
max(shift)
for employee
in (
1,2,3,4
)
)
所以我这样做了,它带回了所有列,而不是旋转格式。 当前结构:
employee | workdate | shift |
---|---|---|
employee1 | date1 | shift1 |
employee1 | date2 | shift2 |
employee2 | date1 | shift1 |
employee2 | date2 | shift2 |
employee3 | date1 | shift1 |
employee3 | date2 | shift2 |
employee4 | date1 | shift1 |
employee4 | date2 | shift2 |
employee5 | date1 | shift1 |
employee5 | date2 | shift2 |
我想要的结果是:
date1 | date2 | date3 | |
---|---|---|---|
employee1 | shift1 | shift2 | shift3 |
employee2 | shift1 | shift2 | shift3 |
employee3 | shift1 | shift2 | shift3 |
employee4 | shift1 | shift2 | shift3 |
感谢您的回答。
你想要:
select employee,
date1,
date2,
date3
from shift
pivot (
max(shift)
for workdate
in (
DATE '2021-08-17' AS date1,
DATE '2021-08-18' AS date2,
DATE '2021-08-19' AS date3
)
)
其中,对于您的示例数据:
CREATE TABLE shift (employee, workdate, shift) AS
SELECT 'employee1', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee1', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee2', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee2', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee3', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee3', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee4', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee4', DATE '2021-08-18', 'shift2' FROM DUAL UNION ALL
SELECT 'employee5', DATE '2021-08-17', 'shift1' FROM DUAL UNION ALL
SELECT 'employee5', DATE '2021-08-18', 'shift2' FROM DUAL;
输出:
EMPLOYEE DATE1 DATE2 DATE3 employee2 shift1 shift2 employee1 shift1 shift2 employee3 shift1 shift2 employee5 shift1 shift2 employee4 shift1 shift2
注意:对于 shift3,您的数据有零行。
db<>fiddle here
如果您需要员工的 where 子句以及工作日期的数据透视表,那么您将编写
SELECT * FROM (
SELECT employee, workdate, shift
FROM shift WHERE employee IN ('employee1', 'employee2', 'employee3')
)
pivot (
max(shift)
for workdate
in (
('date1','date2','date3','date4')
)
)