Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8690087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:49:39+00:00 2026-06-12T23:49:39+00:00

I have a query which calculates the total hours worked for the current week

  • 0

I have a query which calculates the total hours worked for the current week by task:

SELECT name AS 'Task Name'
       , sum(hours) AS `Total Hours`
        FROM time_records
        WHERE yearweek(record_date, 3) = yearweek(now(), 3)
        GROUP BY
        weekday(record_date), name

The WHERE clause specifies the current week:

WHERE yearweek(record_date, 3) = yearweek(now(), 3)

This data is displayed as a table using the MySQL query and a PDO statement.

I would now like to add an option for the viewer to see the previous week of data by clicking a link under the table (e.g., “Previous Week”). In order to show that data, I would need to update the WHERE clause to query for the previous week:

   WHERE yearweek(record_date, 3) = yearweek(now(), 3) - 1

I’m wondering what the best way to do this is? I would prefer to keep just one version of the overall query. One thought is to use PHP to add the “- 1” to the query, such as:

WHERE yearweek(record_date, 3) = yearweek(now(), 3) <?php if (prev) {echo "- 1" ;} ?>

I think this should work, but how can I determine the prev condition? Should I make the entire query be inside a function so that for the default it can be “Time_hours(0)” and for the previous it can be “Time_hours(1)” – or is there some better way?

I’m concerned about keeping the code manageable and following best practices. Any tips would be appreciated. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:49:40+00:00Added an answer on June 12, 2026 at 11:49 pm

    I suggest you try a query that has a parameter for the number of weeks ago you want to process. YEARWEEK is a bad choice for weekly processing because it handles end-of-year rollovers poorly.

    Try this, if your weeks begin on Sunday. This will give the current week.

    select r.record_id, r.record_date, w.week weekstart
    from record r
    join (
        select 
          (FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) 
               - interval 0 week) week
     )w
    where record_date >= w.week
      and record_date < w.week + interval 1 week
    

    Fiddle: http://sqlfiddle.com/#!2/4c487/1/0

    If you want, let’s say, one week ago, change - interval 0 week to - interval 1 week in your query. See this fiddle. http://sqlfiddle.com/#!2/4c487/2/0

    You can put an an arbitrary number of weeks ago, even weeks that span year ends. See here for interval -42 week for example. http://sqlfiddle.com/#!2/4c487/3/0

    The expression FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -1, 7)) rounds the current date down to the previous Sunday. If you want to round it to the previous Monday, use FROM_DAYS(TO_DAYS(CURDATE()) -MOD(TO_DAYS(CURDATE()) -2, 7)). For example: http://sqlfiddle.com/#!2/4c487/8/0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
I have a query which looks like this: SELECT LossCost, CoverageID FROM BGILossCost] WHERE
I have this query which works perfectly: SELECT cp.* FROM CustPrimaryQ cp JOIN Customer
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
I have a query which returns a series of cells of data from a
I have an sql query which calculates the number of distinct fileIds in a
I have the following queries which calculates the total order sale happened in last
I have the following query which calculates today's midnight value (UTC) as a datetime:
I have created an MDX query which calculates the TOP 10 ZipCodes (according to
I have a MySQL SELECT query which uses 20 different comparisons within the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.