Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Solving a SQL problem without using an OLAP function

Status
Not open for further replies.

kdbenson

Programmer
Jan 7, 2009
11
0
0
US
Hi,

I have a SQL problem that I have solved using an OLAP function but I am wondering if there is a better way to do it - more elegant with potentially better performance. Here is the essence of the problem. Each person must take a test once per day over a specified period of time. They may take the test more than once per day, but must take the test at least once. During the course of the day they can also move around to different rooms. The data would look something like this for one person:

Person_Name Location Test_Date Test_Time
Smith Rm. A 10/21/2010 10/21/2010 13:00
Smith Rm. A 10/22/2010 10/22/2010 12:00
Smith Rm. B 10/22/2010 NULL
Smith Rm. B 10/23/2010 10/23/2010 10:00
Smith Rm. C 10/23/2010 10/23/2010 12:00
Smith Rm. D 10/23/2010 NULL

I want to return all the tests that were taken even there are more than one in a day. However, I don't want to return a record for a room if there was no test given in that location but there was a test given on that day (10/23/2010 in the example above). However, if there was no test given on a day, I still want to return a record for that day. Based on the sample above what I want to return is:

Person_Name Location Test_Date Test_Time
Smith Rm. A 10/21/2010 10/21/2010 13:00
Smith Rm. A 10/22/2010 10/22/2010 12:00
Smith Rm. B 10/22/2010 NULL
Smith Rm. B 10/23/2010 10/23/2010 10:00
Smith Rm. C 10/23/2010 10/23/2010 12:00

I solved this by ranking over the person_name and test_date and ordering descending on test_time. The code looked sometihng like this:
Code:
SELECT person_name, location, test_date, test_time,
RANK () OVER (PARTITION BY person_name, test_date ORDER BY test_time DESC) AS rec_rank
from table_1
QUALIFY NOT (test_time IS NULL AND rec_rank > 1);
This works but it just seems like there should be a simpler way. OLAP functions can be performance intensive and I am dealing with millions of records in the real world. Any ideas?

Thanks,
Kevin
 
However, if there was no test given on a day, I still want to return a record for that day.

You'll need a calendar table to return dates not found in you table.


BTW, I can't see why you want to return
Smith Rm. B 10/22/2010 NULL
but not
Smith Rm. D 10/23/2010 NULL

What's the difference I don't understand?
 
You are correct, there is no difference. I should have proof-read it more clearly. The second record in both recordsets above should be excluded. I meant to have only one record from 10/22 and for that record to have a NULL test_time.

As to your point about a calendar table, I actually am using it to derive the test_date, but to simplify this example I just included it as a separate column.
 
As simple as this?
[tt]
SELECT * FROM table
WHERE Test_Time IS NOT NULL
UNION ALL
SELECT * FROM table
WHERE Test_Time IS NULL
AND Test_Date NOT IN (SELECT Test_Date FROM table
WHERE Test_Time IS NOT NULL)[/tt]


Or did I miss something?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top