Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > General > Database Concepts & Design > How to optimize this design?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-17-07, 06:16
miserableman miserableman is offline
Registered User
 
Join Date: Sep 2007
Posts: 2
How to optimize this design?

Hey everyone,

I've got a very simple database with a single table:

Quote:
EVENT_TABLE
-------------
description
start_date

Now what I want to do with the contents of this table is display the latest(events with start_dates furthest in the future) N dates on which events occur. For example, with N=2 and assuming there's some data in the table I would like to output something similar to the following:

Quote:
Wednesday, September 26, 2007
--------------------------------
1. event description
2. event description
3. event description


Monday, September 24, 2007
--------------------------------
1. event description
2. insert event description

Now I need to
1) somehow figure out what the most recent dates with events are
2) for each of those dates get the events and display them

Seems like a lot of queries would need to be made to accomplish such a seemingly simple task... I'm quite new to the db world and would appreciate any thoughts on how I could improve this design

Thanks
Reply With Quote
  #2 (permalink)  
Old 09-17-07, 07:39
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
This question really belongs in the SQL forum (assuming you are using SQL?) or the appropriate DBMS forum since the exact SQL to do this will depend on your DBMS. Some have constructs like "TOP(N)" or "ROWNUM <= N" to get the first N rows from a query. The SQL I have shown below is less efficient but should work on any DBMS:

1) To get the latest N dates in the table:

Code:
select distinct start_date from events e1 where N > (select count(distinct start_date) from events e2 where e2.start_date > e1.start_date );

NOTE: I am assuming start_date is a pure date with no time component. If it includes a time component you need to remove that - e.g. in Oracle you would use TRUNC(start_date).

2) To get all the data for those dates:

Code:
select * from events where event_date in ( select distinct start_date from events e1 where N > (select count(distinct start_date) from events e2 where e2.start_date > e1.start_date ) );

Again, you'll need to ignore time components if they exist.

You only need query (2): I just showed query (1) to explain how the method works.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 09-17-07, 08:22
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 13,556
and to answer your original question, "How to optimize this design?" -- you can't, it's as optimal as it can be

the only thing that's up for debate is whether your PK is description (each event may occur only once), start_date (each date may have only one event), or the composite {description,start_date} (each event may occur only once on each date)
__________________
r937.com | rudy.ca

pre-order my book Simply SQL from Amazon
Reply With Quote
  #4 (permalink)  
Old 09-30-07, 01:29
miserableman miserableman is offline
Registered User
 
Join Date: Sep 2007
Posts: 2
Thanks alot, I appreciate the replies. I have a question about this SQL though:

Quote:
Originally Posted by andrewst
1) To get the latest N dates in the table:

Code:
select distinct start_date from events e1 where N > (select count(distinct start_date) from events e2 where e2.start_date > e1.start_date );

How exactly does this work? The subquery with "e2.start_date > e1.start_date" is especially confusing to me.
Reply With Quote
  #5 (permalink)  
Old 09-30-07, 08:16
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
It's called a "correlated subquery", which means it is evaluated for each row of the main query. It works a bit like this (conceptually):
Code:
for e1 in (select distinct start_date from event) loop select count(distinct start_date) into cnt from events e2 where e2.start_date > e1.start_date; if N > cnt then output e1.start_date; end if; end loop;
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On