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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:28:22+00:00 2026-05-23T11:28:22+00:00

I have a problem where I have 4 days as input, and I must

  • 0

I have a problem where I have 4 days as input, and I must get values for their last 4 weeks for each

This last 4 weeks does not mean the most 4 recent weeks for which I have the solution.

    SELECT prodno, 
           ardate8n, 
           selloff1 
    FROM   sales s 
           JOIN ( SELECT CAST(DATE_SUB('2011-02-27', INTERVAL 1 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-27' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-27', INTERVAL 2 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-27' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-27', INTERVAL 3 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-27' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-27', INTERVAL 4 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-27' AS adt

UNION ALL

 SELECT CAST(DATE_SUB('2011-02-26', INTERVAL 1 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-26' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-26', INTERVAL 2 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-26' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-26', INTERVAL 3 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-26' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-26', INTERVAL 4 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-26' AS adt

UNION ALL

 SELECT CAST(DATE_SUB('2011-02-25', INTERVAL 1 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-25' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-25', INTERVAL 2 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-25' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-25', INTERVAL 3 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-25' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-25', INTERVAL 4 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-25' AS adt

UNION ALL

 SELECT CAST(DATE_SUB('2011-02-24', INTERVAL 1 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-24' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-24', INTERVAL 2 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-24' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-24', INTERVAL 3 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-24' AS adt
 UNION ALL
 SELECT CAST(DATE_SUB('2011-02-24', INTERVAL 4 WEEK) AS DECIMAL(8,0)) AS wdt, '2011-02-24' AS adt) days 
             ON s.ardate8n = days.wdt 
    WHERE  custno = 38726 
           AND deptno = 0 
           AND Find_in_set(prodno, '0020,0064,0070,0073,0096') > 0 
    ORDER by prodno,adt,ardate8n;

As you see I have the most recent 4 weeks hardcoded. This only reads most recent 4 weeks per product/per entry date, and if one or more weeks are missing records I don’t get 4 rows.

So I need to get this dynamically with some kind of limit involved in this. The sales.ardate8n gives if there is record for a given day.

This returns the following data

0006, '2011-03-03', 20110127, 0
0006, '2011-03-03', 20110203, 0
0006, '2011-03-03', 20110210, 0
0006, '2011-03-04', 20110128, 0
0006, '2011-03-04', 20110204, 0
0006, '2011-03-05', 20110129, 0
0006, '2011-03-05', 20110205, 0
0006, '2011-03-05', 20110212, 0
0006, '2011-03-05', 20110219, 0

As you see for entry date 2011-03-03 a product has only 3 rows for same weekday.
and for entry date 2011-03-04 a product has only 2 rows for same weekday.

  • 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-05-23T11:28:22+00:00Added an answer on May 23, 2026 at 11:28 am

    Although a comma-separated list is a convenient way of passing a set of values to the query, it is unfit in situations like this one, where it is required that every value requested should be present in the dataset. So instead of being used as a list, the values should be made a data column, a row set. That would make it possible to include all of them in the resulting set.

    The above applies to the list of prodno values in your query, but same goes for dates.

    Here’s an example of how it would be possible to meet the requirements if the input data were made into datasets:

    SELECT
      p.prodno,
      d.date,
      CAST(DATESUB(d.date, INTERVAL (w.weeksAgo) WEEK) AS DECIMAL(8, 0)) AS ardate8n,
      s.selloff1
    
    FROM       (SELECT @date1 AS date
      UNION ALL SELECT @date2
      UNION ALL SELECT @date3
      UNION ALL SELECT @date4) d
    
    CROSS JOIN (SELECT 1 AS weeksAgo
      UNION ALL SELECT 2
      UNION ALL SELECT 3
      UNION ALL SELECT 4) w
    
    CROSS JOIN (SELECT '0020' AS prodno
      UNION ALL SELECT '0064'
      UNION ALL SELECT '0070'
      UNION ALL SELECT '0073'
      UNION ALL SELECT '0096') p
    
    LEFT JOIN sales s
      ON s.ardate8n = CAST(DATESUB(d.date, INTERVAL (w.weeksAgo) WEEK) AS DECIMAL(8, 0))
     AND s.prodno   = p.prodno
     AND s.custno   = 38726 
     AND s.deptno   = 0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this problem for 2 days now and it's an
This problem has been kicking my butt for a few days now. I have
I have been stuck on this problem for days and am about the crack...
I have bunch of employees, each employee have 7 days for their work schedule.
I have been trying for last 3 days still i am not able to
in the last few days I've been thinking about this problem without finding an
I have been stuck on this problem for almost 2 days now, consider the
I have been banging my head against this problem for days, and searched exhaustively
I have been working for a couple of days on a problem with my
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE

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.