×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Solving a SQL problem without using an OLAP function

Solving a SQL problem without using an OLAP function

Solving a SQL problem without using an OLAP function

(OP)
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
 

RE: Solving a SQL problem without using an OLAP function

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?

RE: Solving a SQL problem without using an OLAP function

(OP)
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.
 

RE: Solving a SQL problem without using an OLAP function

As simple as this?

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)



Or did I miss something?

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close