Jump to content

I hate dates, the worst is adding and subtracting them.


virtuexru

Recommended Posts

Hey guys. Mondays suck. I've been asked to write a script to determine how many vehicles have been added by employees in the past WEEK. So basically, I would need a count on employees for who had the most vehicles posted in the past one week.

 

My database is setup like this:

 

Row Name: _author can contain any persons e-mail for example:  "person1@whatever.com" or "person3@whatever.com".

 

Row Name: _added is the DATE the person posted the vehicle in this format: YYYY-MM-DD.

 

Now, how would I go about counting who had the most posts in the past week? Anyone have any ideas? I've tried a few things with mktime but alas, no avail.

 

Here is my SQL for vehicle postings for ALL-TIME (not one week) just for reference:

 

$count  = "SELECT DISTINCT _author, COUNT(_author) AS count FROM dbname GROUP BY _author ORDER BY count DESC LIMIT 10";

Link to comment
Share on other sites

What is the data type of _added?

 

 

 

Anyway, assuming it's varchar (ewww..... use date or something else), you could do this:

 

 

SELECT _author, COUNT(*) as total_vehicles FROM table WHERE WEEKOFYEAR(DATE(_added)) = WEEKOFYEAR(NOW())-1 AND YEAR(DATE(_added)) = YEAR(NOW());

 

 

Question, by the way....  Do you want a week ago from the current day, or do you want a week as in Monday-Sunday (or Sunday - Saturday depending on how you look at it)?

 

 

I'm hoping someone will have a better query...  one that takes everything into account lol.

 

 

If you want a week from the current day, that's easy though.

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.