Solved

Develop a query exploding values in a column and comparing

Posted on 2013-02-03
5
213 Views
Last Modified: 2013-02-05
I have a table contest_setup and it contains (simplified)

id
account_id
ContestName
ContestStartDate

the ContestStartDate column contains dates that a particular contest will run ie:
2013-02-18 00:00:00,2013-02-19 04:00:00,2013-02-20 16:00:00
(this contest would run on 3 different dates for example)

Some rows may contain just one date, others a few like above.

My question is: how can I construct a query to check for all the Contests that may be running on 2013-02-19 (from 00:00:00 to 23:59:59) and arrange them by date/time soonest to latest?

I'm guessing I need to go through the rows that belong to a certain account_id that would be passed to it (for example '17'),

I need to explode the ContestStartDate on the comma and check the values for the date I'm interested in and somehow 'magically' display them earliest to latest.

Can anyone throw me a bone to get going in the right direction? Its beyond my ability currently.
0
Comment
Question by:tjyoung
  • 3
  • 2
5 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38849630
Make a Google search for "Should I normalize my database" and read the interesting opinions on either side of the idea.  In the instant case, the answer for you is "Yes, I must normalize."  Any column that contains more than one atomic data element is a recipe for catastrophe.  So create a "junction table" to connect your contest_setup table to your contest_dates table.  Put all the dates in a row of the contest_dates table, using the column data type of DATETIME.
0
 
LVL 1

Author Comment

by:tjyoung
ID: 38854192
Hi,
changed my approach but not sure about the junction table or why to use one?
Here is where I'm at now:

2 tables
contest_setup - id (auto), contestname, account_id
contest_dates - contestsetup_id, contestdate(datetime)

So I'm thinking I would be able to query contests slated for today for example by joining the tables and matching up the id and the contestsetup_id where its todays date and the account_id is 'x'

That right? I wasn't sure how or why you would introduce a junction table.

I haven't got to querying the tables etc. yet so if there is a fatal flaw, I may have just not come across it at this point.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38854519
I don't see anything wrong with that design.  You may find that you do not need a junction table and you can just use the JOIN clause.  Be sure to index any columns used in JOIN, just as you would index any columns used in WHERE, and add LIMIT statements if possible.

Best regards, ~Ray
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 38854668
very good advice.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38854734
Thanks for the points, it's a great question, ~Ray
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question