I am sorry for asking this seemingly trivial question, but I have been struggling with it for some time, my deadline is approaching and I can't find any answers for it.
I have 3 tables:
Calendar table:
CREATE TABLE "CJ_CAL"
( "CAL_ID" NUMBER NOT NULL ENABLE,
"CAL_DATE" DATE,
CONSTRAINT "CJ_CALENDAR_PK" PRIMARY KEY ("CAL_ID")
)
Employee table
CREATE TABLE "CJ_EMP"
( "EMP_ID" NUMBER NOT NULL ENABLE,
"FIRST_NAME" VARCHAR2(40),
"LAST_NAME" VARCHAR2(40),
"DEPARTMENT" VARCHAR2(10),
"STATION" VARCHAR2(10),
"MANAGER" VARCHAR2(40),
"WORK_HOURS" NUMBER,
"SCHEDULE" VARCHAR2(400),
CONSTRAINT "CJ_EMP_PK" PRIMARY KEY ("EMP_ID")
)
and
Attendance table
CREATE TABLE "CJ_EMP_CAL"
( "EMP_CAL_ID" NUMBER NOT NULL ENABLE,
"CAL_ID" NUMBER NOT NULL ENABLE,
"EMP_ID" NUMBER NOT NULL ENABLE,
"STATUS" NUMBER,
"OVERTIME" NUMBER,
"REMARK" VARCHAR2(400),
CONSTRAINT "CJ_EMP_CAL_PK" PRIMARY KEY ("EMP_CAL_ID")
)
cj_emp_cal references cj_cal and cj_emp using a foreign key.
I need to create an interactive report where I will display all the employees in separate rows and have columns for each day of a selected month. All the employees have been 'assigned' a working day in the CJ_EMP_CAL table and I need to display the status (A - Absent, P - Present, OT - Overtime, V - vacation) in the report for each day and each employee respectively.
I also need to add two more columns as data for each employee (cj_emp.department and cj_emp.station).
Thank you for being a wonderful resource!
Best regards,
Gordan
So, if an employee has no entry in CJ_EMP_CAL, does that mean they should be shown as absent? Or something else?
In any case, it sounds like you need to show all the rows from CJ_CAL in the timeframe. Then any related rows in the other tables. So you need to outer join them all together.
Which gives a query like:
select c.cal_date, s.stat_name, ec.emp_id, e.department , e.station
from CJ_CAL c
left join CJ_EMP_CAL ec
on c.cal_id = ec.cal_id
and ec.emp_id in (1, 2, 3, 4)
left join CJ_EMP e
on e.emp_id = ec.emp_id
left join CJ_STATUS s
on ec.status = s.stat_id
where c.cal_date >= date'2018-02-01'
and c.cal_date < date'2018-02-08'
order by ec.emp_id, c.cal_date;
CAL_DATE STAT_NAME EMP_ID DEPARTMENT STATION
01-FEB-2018 06:08:04 OVERTIME 1 GSE YHM
02-FEB-2018 06:08:04 PRESENT 1 GSE YHM
03-FEB-2018 06:08:04 OVERTIME 1 GSE YHM
04-FEB-2018 06:08:04 ABSENT 1 GSE YHM
05-FEB-2018 06:08:04 OVERTIME 1 GSE YHM
06-FEB-2018 06:08:04 PRESENT 1 GSE YHM
07-FEB-2018 06:08:04 PRESENT 1 GSE YHM
01-FEB-2018 06:08:04 VACATION 2 GSE YHM
02-FEB-2018 06:08:04 ABSENT 2 GSE YHM
03-FEB-2018 06:08:04 OVERTIME 2 GSE YHM
04-FEB-2018 06:08:04 OVERTIME 2 GSE YHM
05-FEB-2018 06:08:04 ABSENT 2 GSE YHM
06-FEB-2018 06:08:04 PRESENT 2 GSE YHM
07-FEB-2018 06:08:04 OVERTIME 2 GSE YHM
01-FEB-2018 06:08:04 ABSENT 3 GSE YHM
02-FEB-2018 06:08:04 OVERTIME 3 GSE YHM
03-FEB-2018 06:08:04 OVERTIME 3 GSE YHM
04-FEB-2018 06:08:04 ABSENT 3 GSE YHM
05-FEB-2018 06:08:04 OVERTIME 3 GSE YHM
06-FEB-2018 06:08:04 PRESENT 3 GSE YHM
07-FEB-2018 06:08:04 VACATION 3 GSE YHM
01-FEB-2018 06:08:04 ABSENT 4 GSE YVR
02-FEB-2018 06:08:04 VACATION 4 GSE YVR
03-FEB-2018 06:08:04 PRESENT 4 GSE YVR
04-FEB-2018 06:08:04 OVERTIME 4 GSE YVR
05-FEB-2018 06:08:04 PRESENT 4 GSE YVR
06-FEB-2018 06:08:04 OVERTIME 4 GSE YVR
07-FEB-2018 06:08:04 ABSENT 4 GSE YVR