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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:46:01+00:00 2026-06-06T16:46:01+00:00

I have been using stackoverflow vastly during the last year – an excellent source

  • 0

I have been using stackoverflow vastly during the last year – an excellent source w/ great contributors. Now it’s my time to request for help.

The setup is normal:
Orders, OrderArticles and Articles

I want to get the total amount of articles sold during the last year, but only during the best 5 weeks.

Never mind the WEEK-function and UNIXTIME-blah blah – I’ve got that covered. My question is wether it’s possible or not to do without resorting to stored procedures or functions.

I have created a subquery for the summary for each week and article and order the result by the sum descendingly. Now – I only have to LIMIT the query to 5. Easy, but I also have to filter the result on the ArticleID BUT since I’m inside a subquery I don’t have access to the outer ArticleID and it doesn’t help to JOIN the result – it’s too late 😉

The syntax (hard to understand w/o the actual sql, right…?)

SELECT a.ID, [more fields], omg.total
FROM Articles AS a
LEFT JOIN
(
    SELECT weeklytotals.articleID, weeklytotals.total
    FROM
    (
        SELECT SUM(ra.quantity) AS total, ra.articleID AS articleID
        FROM OrderArticles ra
        INNER JOIN Orders r
            ON ra.orderID = r.ID
        WHERE r.timeCreated >= UNIX_TIMESTAMP('2011-06-30')
        GROUP BY ra.articleID, WEEK(FROM_UNIXTIME(r.timeCreated))
        ORDER BY SUM(ra.quantity) DESC
    ) AS weeklytotals
    WHERE omg.articleID = a.ID --<-- THIS IS NOT WORKING BUT NECESSARY!
    LIMIT 0, 5
) AS omg
ON omg.articleID = a.ID
WHERE a.isEnabled = 1 --more WHERE-thingys 

This here returns the top 5 articles and ties the them to the correct Article. yay.

I have left out the SUM-function (which could go into the omg-SELECT).

Do you understand? Do I understand what I want? Yes, of course we do!

Thanx in advance.

Edit: The conditions have been changed – which makes my life easier, but I still would like to know if there is a solution to the problem.

  • 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-06T16:46:02+00:00Added an answer on June 6, 2026 at 4:46 pm

    If you require the omg subquery to use data from the a table, place it into the SELECT part not the FROM part. Using terms from the mysql documentation, you want the result of a correlated subquery to appear as a scalar operand in the outer result set.

    You wrote about being interested in the sum, i.e. only a single number per article, although you left out the SUM from your example query. My approach relies on that sum, and would probably break in a bad way if you really needed distinct values for each of the best five weeks.

    SELECT a.ID, [more fields], IFNULL(SUM(
        (
            SELECT SUM(ra.quantity) AS total
            FROM OrderArticles ra
            INNER JOIN Orders r
               ON ra.orderID = r.ID
            WHERE ra.articleID = a.ID -- <-- reference a.ID here
              AND r.timeCreated >= UNIX_TIMESTAMP('2011-06-30')
            GROUP BY WEEK(FROM_UNIXTIME(r.timeCreated))
            ORDER BY SUM(ra.quantity) DESC
            LIMIT 0, 5
        )), 0) AS total
    FROM Articles AS a
    WHERE a.isEnabled = 1 --more WHERE-thingys 
    GROUP BY a.ID
    

    I’m not saying anything about performance here. Placing the subquery this way, it will be executed for every row of the result set. So it might be too slow for practical use if you have a large number of articles. But if that should happen, I doubt that stored procedures or similar tricks would fare any better.

    Edit: I found out that my original suggestion, which used subquerys nested two levels deep, doesn’t allow access the innermost subquery to use a column of the outermost. But toying with this on sqlfiddle I also found out that one may safely pass the result of a subquery to sum, thus avoiding one level of nesting. So the above code nos has actually been checked and executed by a MySQL server, and should therefore work as intended.

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

Sidebar

Related Questions

[i meant to ask this question on StackOverflow...] I have been using VisualSVN Server
I have been using Visual Studio integrated with Source Safe for years, when I
I have been using this code with great success to pull out the first
I have been using the CodeIgniter system for a while now - but it
I have been using WPF for a few years now and don't have any
I have been using Weblogic as my company used it. Now I want to
I have been using attachment_fu on a project for a long time and all
I have been using Stackoverflow for a while to find solutions to my programming
I have been using Java for a long time, and for quite some time
I have been using php for a while but haven't taken the time to

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.