Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-18-2013, 04:21 PM   PM User | #1
Bobafart
Regular Coder

 
Join Date: Dec 2006
Posts: 417
Thanks: 168
Thanked 1 Time in 1 Post
Bobafart is on a distinguished road
mySQL query for every 4 week block in a calendar week?

I have a table with about 5 years of dates (2011 - 2016).

Each date is stored in a column called "dt" as YYY-MM-DD (ie. 2012-01-04) and "y" as year (2012, 2013). I also have a column called "w" which is the week number for each week in the calendar year which helps. It resets to 0 for the first week of each year and then increments to 52 to the last week.

I am trying to find the correct MYSQL SELECT query to extract 4 week blocks (pay periods) from each calendar year.

So for 2014, one would expect 13 pay blocks.. I want to return the results as follows:

Period 1
Week 1 (2012-12-31 to 2012-01-05)
Week 2 (2012-01-06 to 2012-01-12)
Week 3 (2012-01...
Week 4 (2012..

Period 2
Week 5 (2012..
Week 6 (2012..
Week 7 (2012..
...

Period 3

..

..

Period 13

..

Code:
$currentYear="2014"
for ($i = 0; $i <= 52; $i++) {
$sql="SELECT * FROM calendar WHERE Y='$currentYear' AND w=$i GROUP BY w";
$result=mysql_query($sql);
while($row=mysql_query($result)){
  ... is this the most effiecient way?  once I figure out the select statement then I will switch the PHP over to MYSQLi

}
}
I am not even sure how to structure the SELECT statement efficiently which I suspect should be resulted in a while loop... what is the most efficient way to do this?
Bobafart is offline   Reply With Quote
Old 01-18-2013, 11:51 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You know, there really was no reason to have the Y and W columns in that table.

MySQL could easily derive both of those from the DT field.

But oh well, since you have them, let's use them.

Anyway, the point you are completely missing is *WHAT* you want to show for each week.

That is, do you want to show the detailed payroll (every paycheck for every person) for each week? Or so you want to show the total payroll for the week? Or what?

No, you CERTAINLY should *NOT* do a FOR loop getting one week at a time. Yuck.

But you need to tell us what data you want to display for each week.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Users who have thanked Old Pedant for this post:
Bobafart (01-19-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:34 PM.


Advertisement
Log in to turn off these ads.