Q
Problem solve Get help with specific problems with your technologies, process and projects.

# Creating a virtual SQL table for date values

## One reader asks how to use SQL to track his company employees' absentee days, and expert Dan Clamage suggests creating a virtual table.

I have two tables:- emp_info,emp_attendance

Table structure:

Emp_Info
empid int, empname varchar(50)

Emp_Attendance
empid int, emp_day datetime

The emp_attendance has the entries of dates for all the employees when they are present like:

Empid    emp_day
101        1-1-2012
101        1-2-2012
101        1-4-2012 //Note: third date is missing as the employee is absent
101        1-5-2012
102        1-1-2012
102        1-3-2012 //Note: Here employee 102 is absent on date 2

I want all the list of employees with their absent dates.

The thing you're missing is a DATE table to generate all dates in a given range.

Some folks build an actual table and populate it with every calendar date from, say, the beginning of their business (or business industry) to some fixed future date (when I suppose they believe they'll be out of business or they expect to completely rewrite their application anyway).

However, it's simpler and makes more sense to create a virtual table. Pipelined functions are perfect for this kind of problem.

Any function has to return an actual SQL datatype; Oracle allows us to define suitable schema-level objects to fulfill this requirement for more complex situations.

We need a collection type for a pipelined function to return. This can be simply a collection of DATE:

CREATE OR REPLACE TYPE nt_date IS TABLE OF DATE;
/

My function will return this collection type.

CREATE OR REPLACE FUNCTION generate_dates(
p_from IN DATE
,p_to IN DATE)
RETURN nt_date PIPELINED
IS
-- normalize inputs to be as-of midnight
v_from DATE := TRUNC(NVL(p_from, SYSDATE));
v_to DATE := TRUNC(NVL(p_to, SYSDATE));
BEGIN
LOOP
EXIT WHEN v_from > v_to;
PIPE ROW (v_from);
v_from := v_from + 1; -- next calendar day
END LOOP;
RETURN;
END generate_dates;
/

Let’s test this first:

-- so we can see the time too
ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-YYYY HH24:MI:SS';

SELECT *
FROM TABLE(generate_dates(SYSDATE, SYSDATE));

COLUMN_VALUE
--------------------
08-JAN-2012 00:00:00

Notice the system-generated name of the column is COLUMN_VALUE. If you prefer being able to provide your own name, then create an object type first, and base your collection on that instead.

CREATE OR REPLACE TYPE obj_date IS OBJECT (
date_val DATE
);
/

CREATE OR REPLACE TYPE nt_date IS TABLE OF obj_date;
/

CREATE OR REPLACE FUNCTION generate_dates(
p_from IN DATE
,p_to IN DATE)
RETURN nt_date PIPELINED
IS
-- normalize inputs to be as-of midnight
v_from DATE := TRUNC(NVL(p_from, SYSDATE));
v_to DATE := TRUNC(NVL(p_to, SYSDATE));
BEGIN
LOOP
EXIT WHEN v_from > v_to;
PIPE ROW (obj_date(v_from));
v_from := v_from + 1; -- next calendar day
END LOOP;
RETURN;
END generate_dates;
/

Note the PIPE ROW command now needs to return the object type, not a scalar type.

SELECT *
FROM TABLE(generate_dates(SYSDATE, SYSDATE));

DATE_VAL
--------------------
08-JAN-2012 00:00:00

A few more test cases are in order…

SELECT *
FROM TABLE(generate_dates(SYSDATE, SYSDATE-1));

no rows selected

SELECT *
FROM TABLE(generate_dates(SYSDATE, SYSDATE+3));

DATE_VAL
--------------------
08-JAN-2012 00:00:00
09-JAN-2012 00:00:00
10-JAN-2012 00:00:00
11-JAN-2012 00:00:00

SELECT *
FROM TABLE(generate_dates(SYSDATE, NULL));

DATE_VAL
--------------------
08-JAN-2012 00:00:00

Without the NVL, the function would never break out of the loop.

Now we can get back to your actual problem. You want to find those dates for which no record is found in your attendance table. Let’s build the tables and populate them…

CREATE TABLE emp_info(
empid INTEGER
,empname VARCHAR2(50)
);

CREATE TABLE emp_attendance(
empid INTEGER
,emp_day DATE
);

INSERT INTO emp_info (empid, empname) VALUES (101, 'Dan Clamage');
INSERT INTO emp_info (empid, empname) VALUES (102, 'Ronak Khandelwal');

INSERT INTO emp_attendance (empid, emp_day) VALUES (101, DATE '2012-01-01');
INSERT INTO emp_attendance (empid, emp_day) VALUES (101, DATE '2012-01-02');
INSERT INTO emp_attendance (empid, emp_day) VALUES (101, DATE '2012-01-04');
INSERT INTO emp_attendance (empid, emp_day) VALUES (101, DATE '2012-01-05');

INSERT INTO emp_attendance (empid, emp_day) VALUES (102, DATE '2012-01-01');
INSERT INTO emp_attendance (empid, emp_day) VALUES (102, DATE '2012-01-03');

COMMIT;

For date literal values, I prefer using the ANSI standard format.

What you’re looking for is the set of all potential attendance dates for each employee, minus the set of actual attendance dates. SQL works with sets of data, so it’s straightforward to describe each set.

Let’s see the first set…

-- potential attendance dates for each employee in a given range
SELECT ei.empid, gd.date_val
FROM emp_info ei
CROSS JOIN
TABLE(generate_dates(DATE '2012-01-01', DATE '2012-01-05')) gd
ORDER BY ei.empid, gd.date_val;

EMPID DATE_VAL
---------- --------------------
101 01-JAN-2012 00:00:00
101 02-JAN-2012 00:00:00
101 03-JAN-2012 00:00:00
101 04-JAN-2012 00:00:00
101 05-JAN-2012 00:00:00
102 01-JAN-2012 00:00:00
102 02-JAN-2012 00:00:00
102 03-JAN-2012 00:00:00
102 04-JAN-2012 00:00:00
102 05-JAN-2012 00:00:00

10 rows selected.

Note the cross join computes the Cartesian Product of the two sets. This potentially will return a huge number of rows. You wouldn’t want to run this for thousands of employees for several years.

Now for their actual attendance…

-- actual attendance dates for each employee in a given range
SELECT ei.empid, ea.emp_day
FROM emp_info ei
INNER JOIN emp_attendance ea
ON ea.empid = ei.empid
WHERE ea.emp_day BETWEEN DATE '2012-01-01' AND DATE '2012-01-05'
ORDER BY ei.empid, ea.emp_day;

EMPID EMP_DAY
---------- --------------------
101 01-JAN-2012 00:00:00
101 02-JAN-2012 00:00:00
101 04-JAN-2012 00:00:00
101 05-JAN-2012 00:00:00
102 01-JAN-2012 00:00:00
102 03-JAN-2012 00:00:00

6 rows selected.

Let’s subtract the latter set from the former set…

SELECT ei.empid, gd.date_val
FROM emp_info ei
CROSS JOIN
TABLE(generate_dates(DATE '2012-01-01', DATE '2012-01-05')) gd
MINUS
SELECT ei.empid, ea.emp_day
FROM emp_info ei
INNER JOIN emp_attendance ea
ON ea.empid = ei.empid
WHERE ea.emp_day BETWEEN DATE '2012-01-01' AND DATE '2012-01-05'
ORDER BY 1, 2;

EMPID DATE_VAL
---------- --------------------
101 03-JAN-2012 00:00:00
102 02-JAN-2012 00:00:00
102 04-JAN-2012 00:00:00
102 05-JAN-2012 00:00:00

Note empid 101 is absent on the day you mentioned, and that empid 102 is absent on not just date 2 as you indicated, but also the 4th and 5th. It’s important to make sure we’re comparing the same timeframe in both sets.

If you want their actual names, you can put all this in an inline view and join it back to the emp_info table.

SELECT i.empid, i.empname, a.absent
FROM emp_info i
INNER JOIN
(SELECT ei.empid, gd.date_val absent
FROM emp_info ei
CROSS JOIN
TABLE(generate_dates(DATE '2012-01-01', DATE '2012-01-05')) gd
MINUS
SELECT ei.empid, ea.emp_day
FROM emp_info ei
INNER JOIN emp_attendance ea
ON ea.empid = ei.empid
WHERE ea.emp_day BETWEEN DATE '2012-01-01' AND DATE '2012-01-05') a
ON a.empid = i.empid
ORDER BY i.empid, a.absent;

EMPID EMPNAME                                            ABSENT
---------- -------------------------------------------------- --------------------
101 Dan Clamage                                        03-JAN-2012 00:00:00
102 Ronak Khandelwal                                   02-JAN-2012 00:00:00
102 Ronak Khandelwal                                   04-JAN-2012 00:00:00
102 Ronak Khandelwal                                   05-JAN-2012 00:00:00

This was last published in January 2012

## Content

Find more PRO+ content and other member only offers, here.

#### Have a question for an expert?

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

#### Start the conversation

Send me notifications when other members comment.

## SearchDataManagement

• ### Hyperledger Fabric offers path to enterprise blockchain future

Blockchain arose from bitcoin, but it's looking to find a place in the enterprise. Frameworks like Hyperledger Fabric could ...

• ### MongoDB 4.0 takes ACID transactions to multi-document level

MongoDB is taking a deeper step into SQL-style processing waters with a 4.0 update that brings increased support for ...

• ### Data lake concept needs firm hand to pay big data dividends

Data lakes pose technology deployment and data management challenges that can leave analytics users high and dry if the ...

• ### AI functionality limited today but could be a game-changer

Limited AI capabilities could soon give way to technology that is truly transformative for enterprises, surpassing the overhyped ...

## SearchSAP

• ### SAP's Barry Padgett on future of SAP Ariba Network

In this Q&A, new SAP Ariba President Barry Padgett discusses the future of procurement and the experience he will bring to Ariba ...

• ### Avoiding SAP indirect access woes requires good faith

Some customers are concerned that SAP will hit them for indirect access licensing fees, but they can avoid trouble if they act in...

• ### ControlPanelGRC app eases Steelcase's compliance pain

When Steelcase's SAP environment grew in size and complexity, it turned to Symmetry ControlPanelGRC to save time, have more ...

## SearchSQLServer

SQL Operations Studio simplifies routine administration of SQL Server and Azure SQL databases, making database development and ...

• ### Meltdown and Spectre fixes eyed for SQL Server performance issues

Microsoft has responded to the Spectre and Meltdown chip vulnerabilities with patches and other fixes. But IT teams need to sort ...

• ### Five SQL Server maintenance steps you should take -- ASAP

Putting off SQL Server administration tasks can lead to database problems. Enact these often-neglected maintenance items to help ...

## TheServerSide.com

• ### How DevOps concepts eluted from cloud computing and service platforms

The popularity of DevOps can be traced back to the emergence of cloud computing. As programmers began scripting their ...

• ### Pluralsight IQ, Stack Overflow boost developer street cred

Tying the Pluralsight IQ skills test to the Stack Overflow Developer Story helps developers measure their technical skills and ...

• ### Why this quantum computing breakthrough is a security risk

Quantum computing will void pretty much all security encryption techniques and open the door to hackers. Here's how to protect ...

## SearchDataCenter

• ### IBM Power9 servers seek market inroads to AI, cloud

IBM follows up its first Power9 server with a raft of systems designed to appeal to a wider array of markets -- most notably, AI ...

• ### Evaluate read-intensive and write-intensive SSD use cases

Consider write wear, performance and other factors when choosing between read-intensive, write-intensive and mixed-use ...

• ### Some hyper-converged infrastructure use cases pose pitfalls

Hyper-converged infrastructure adoption is skyrocketing, but that doesn't mean that the technology is the best choice for every ...

## SearchContentManagement

• ### Content management in the cloud a main theme in 2018

The future of content management resides in the cloud and with AI, as several 2018 conferences will assure you.

• ### Six things to know about today's SharePoint implementations

As companies migrate their on-premises Microsoft SharePoint sites to the cloud, here are some things they should know about the ...

• ### Upgrades for the SharePoint Online portal

As more organizations migrate SharePoint sites to the cloud, Microsoft has increased at-a-glance dashboard data and analytics to ...

## SearchHRSoftware

• ### Social media key to future of learning and development

Employees aren't just wasting time on social media; some are learning the career skills that will take them to the next level. ...

• ### Limeade releases new employee engagement software

HR tech news roundup: Employee engagement software vendor Limeade expands platform and releases inclusion module for large ...

• ### Don't overlook the many benefits of Microsoft Excel for HR

The maligned spreadsheet tool is no substitute for enterprise apps like HRMS and people analytics, but it will do in a pinch and ...

Close