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 8832869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:38:21+00:00 2026-06-14T08:38:21+00:00

My current query: Select `userSubUnitsID`, `userSubUnits`.`userID`, CONCAT(`u`.`fname`,’ ‘,`u`.`lname`) AS `full`, `pID`, SUM(`quantComp`) AS `total`

  • 0

My current query:

    Select `userSubUnitsID`, `userSubUnits`.`userID`, CONCAT(`u`.`fname`,' ',`u`.`lname`) AS `full`, `pID`, SUM(`quantComp`) AS `total`
    From `userSubUnits`
    JOIN `u` ON `u`.`userID` = `userSubUnits`.`userID`
    GROUP BY `userID`

What I need is to get this by a date. So it will look more like this.

    +-----+------------+----------+-------------+
    |user |current week|prev week |two weeks ago|
    +-----+------------+----------+-------------+
    |John |         564|       354|          687|
    +-----+------------+----------+-------------+
    |Paul |         451|       328|          845|
    +-----+------------+----------+-------------+
    etc

Is this possible with a single query? I know how to limit it for the current week but not how to add in the previous weeks.

FYI, Weeks end Sunday at midnight.
Thanks in advance

  • 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-14T08:38:22+00:00Added an answer on June 14, 2026 at 8:38 am
    SELECT user, SUM(WEEKOFYEAR(datefield) = WEEKOFYEAR(now()) AS current_week,
        SUM(WEEKOFYEAR(datefield) = WEEKOFYEAR(now() - INTERVAL 1 WEEK)) AS last_week,
        SUM(WEEKOFYEAR(datefield) = WEEKOFYEAR(now() - INTERVAL 2 WEEK)) AS two_weeks_ago
    FROM ...
    GROUP BY WEEKOFYEAR(now())
    

    ugly, but should do the trick. Note that this will fail if your dates span a year boundary. To get around that, you’d have to add in some extra year-based logic.

    As well, you’d be better off doing this kind of transformation client-side. It’s ugly to expand, and will very quickly get hideously inefficient if you need to start doing more than just a few weeks, e.g:

    SELECT user, YEAR(datefield), WEEKOFYEAR(datefield), COUNT(*)
    FROM ...
    GROUP BY YEAR(datefield), WEEKOFYEAR(datefield)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table structure: country season points Current query: SELECT SUM(points) AS total, country FROM table
Here's my current query: SELECT IFNULL(sum(open_for), 0) total, count(IF(open_for > 0, 1, null)) wins,
My current query reads: SELECT entry_id, user_id, cat_id, AVG( rating ) as avg_rate FROM
the current SQL query works fine locally on MAMP 1.8.4 running MySQL 5.1.37. SELECT
My current query: SELECT DACTIONMILLIS, DACTIONDATE INTO WF_DACTIONMILLIS, WF_DACTIONDATE FROM WORKFLOWHISTORY WHERE ddocname=? and
This is my current query: SELECT DISTINCT t.* FROM filter AS f INNER JOIN
with my current query and loop: $sched = mysql_query(SELECT * FROM `shows` ORDER BY
My current query: SELECT Series.*, Episodes.* FROM Series, Episodes WHERE EpisodeAirDate > '.time().' AND
This is my current query: select serial_number, order_number, (select TOP 1 PRODUCT_ID from PS_ORD_LINE
My current query:- select p.id as product_id, p.product_name, child.name as category_name, parent.name as parent_category_name

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.