Jump to content

Comparing Records by Dates that are Stored in a Single Table


suttercain

Recommended Posts

Hi everyone,

 

Artacus was nice enough to help me with an issue last week when comparing data from two separate tables using a union. See: Original Post

 

Now I want to do the same thing, kind of, using a single table in lieu of two tables. I need to compare id's based on the date the id was added or removed.

 

Example of Table

 

------------------

| ID | Date          |

| 1  | 09-01-2009 |

| 2  | 09-01-2009 |

| 3  | 09-01-2009 |

| 1  | 09-02-2009 |

| 3  | 09-02-2009 |

| 4  | 09-02-2009 |

| 5  | 09-02-2009 |

-------------------

 

So the MySql result would need to state: Id 2 was removed on 09-02-2009. Id 4 and 5 were added on 09-02-2009.

 

Any help or suggestions would be greatly appreciated. Thanks in advance.

Link to comment
Share on other sites

I'm not sure what you mean by added and removed. I'm guessing what you really need are min and max dates for an id.

 

SELECT id, MIN(date) AS added_on, 
  -- Not sure if you need the last day it WAS in your result
  -- or the first day it WAS NOT
  MAX(date) + 1 AS removed_on 
FROM example
GROUP BY id

Link to comment
Share on other sites

Hi Artacus,

 

Basically need the same 'comparison' like I did before when it was between two tables, but this time using just one table. The output result of the table I posted above should state something similar to this:

 

2 was removed on 09-02-2009.

4 was added on 09-02-2009.

5 was added on 09-02-2009.

 

So ya, taking the most recent date, and comparing the id's to the previous date. Comparing any changes made between the two dates and listing them similar to that of the list above.

 

Thanks again for your time!

 

-SC

 

Link to comment
Share on other sites

This isn't the greatest design. Always try to keep the start and end times for an event in a single row.

This would be ideal for a full outer join but I'm pretty sure MySQL still doesn't support them.

 

SELECT CASE WHEN yesterday.id IS NULL AND tomorrow.id IS NULL THEN 
  concat(today.id, ' was added ', today.date, ' and removed ', tomorrow.date)
  WHEN yesterday.id IS NULL THEN concat(today.id, ' was added ', today.date)
  WHEN tomorrow.id IS NULL THEN concat(today.id, ' was removed ', tomorrow.date)
  ELSE 'Artacus freaking rocks'
END AS status_update
FROM example today 
LEFT JOIN example yesterday ON today.id = yesterday.id
  AND today.date = yesterday.date + INTERVAL 1 DAY
LEFT JOIN example tomorrow ON today.id = tomorrow.id
  AND today.date = tomorrow.date - INTERVAL 1 DAY
WHERE yesterday.id IS NULL OR tomorrow.id IS NULL

Link to comment
Share on other sites

This isn't the greatest design. Always try to keep the start and end times for an event in a single row.

 

Thanks! Ya, I agree sadly there is no "end time" per say because if an id isn't added or removed from a specific date to the next, it's a continuation or a "no change."

 

I ran the code and I got the following out put when echoing 'status_update'

 

3 was added 2009-09-01 -

1 was added 2009-09-01 -

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.