我有以下3个表格 1- EmpLoyees as:
EmpID EmpName -------------- 100 Adam 101 Smith
2-转移为:
ShiftID ShiftName FromTime ToTinme ---------------------------------------- 1 Shift1 9 AM 2 PM 2 Shift2 3 PM 5 PM
3-EmpShifts as:
EMPID SHIFTID Date --------------------------------- 100 1,2 01/12/2017 100 2 02/12/2017 101 1 01/12/2017 101 1,2 02/12/2017
我需要创建一个select语句来获取数据:
EMPID EmpName Date ShiftID ShiftName ShiftTimes ------------------------------------------------------------------------ 100 Adam 01/12/2017 1 Shift1 100 Adam 01/12/2017 2 Shift2 100 Adam 02/12/2017 2 Shift2 101 Smith 01/12/2017 2 Shift2 101 Smith 02/12/2017 1 Shift1 101 Smith 02/12/2017 2 Shift2
那么如何获得。
答案 0 :(得分:1)
正如评论中所提到的,这是一种糟糕的数据格式。将列表存储为字符串是不好的。以下是一些原因:
shiftid
)的列应包含相同的内容。那就是说,你可以用like
之类的东西做你想做的事情来进行比较。以下查询显示了您需要的构造:
select . . .
from empshifts es join
shifts s
on ',' || shiftid || ',' like '%,' || s.shiftid || ',%' join
employees e
on e.empid = es.empid;
答案 1 :(得分:0)
我没有运行查询但它可以正常运行
with shifids as (
select empid,date,regexp_substr(SHIFTID,'[^,]+', 1, level)shifid from EmpShifts
connect by regexp_substr(SHIFTID, '[^,]+', 1, level) is not null
)
select EmpLoyees.empid,EmpLoyees.empname,
shifids.date,shifids.shifid,
Shifts.ShiftID,Shifts.ShiftName
from
EmpLoyees inner join
shifids
on EmpLoyees.empid=shifids.empid
inner join Shifts on Shifts .shiftid=shifids.shifid
答案 2 :(得分:0)