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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:36:44+00:00 2026-06-14T14:36:44+00:00

I do the statistic function for my project. I have the order table with

  • 0

I do the statistic function for my project. I have the order table with some fields as:

  • prouct_id,
  • amount,
  • order_date.

The customer ask me to do the auto scale statistic as: I get the MIN(order_date) of a produc_id and the current date to calculate the days:

  • If the days about ~ 1 month ==> show the statistic of the product by weeks

  • If the days about ~ 1 year ==> show the statistic of the product by months

  • If the days >= 2 year ==> show the statistic of the product by year

I hope that can receive the ideas, examples from all of you about making the statistic as above.

  • 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-14T14:36:45+00:00Added an answer on June 14, 2026 at 2:36 pm

    You didn’t specify what RDBMS is this. So let me assume that it is SQL Server, and you can do this:

    ;WITH MinDates AS
    (
        SELECT 
          produc_id,
          MIN(order_date) order_date
        FROM products
        GROUP BY produc_id
    ), DatesWithIntervals AS
    (
        SELECT 
          product_id,
          order_date,
          CASE 
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 31      THEN 1
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 365     THEN 2
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 365 * 2 THEN 3 
            ELSE 4
          END "Interval"
        FROM MinDates
    )
    SELECT
      product_id,
      order_date
      ...
    FROM DatesWithIntervals 
    --Do your statics here
    

    You didn’t specify what statistics do you want to compute. But you should be able to do whatever statistics you want to do using the last cte: DatesWithIntervals depending on the field Interval and I will leave it as a practice for you.

    Edit: For MySQL, just replace all these CTEs with subqueries like so:

    SELECT
      product_id,
      order_date
          ...
    FROM
    (
        SELECT 
          product_id,
          order_date,
          CASE 
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 31      THEN 1
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 365     THEN 2
            WHEN ABS(DATEDIFF(dd, order_date, GETDATE())) < 365 * 2 THEN 3 
            ELSE 4
          END "Interval"
        FROM
        (
             SELECT 
              produc_id,
              MIN(order_date) order_date
             FROM products
             GROUP BY produc_id  
        ) MinDates
    ) DatesWithIntervals 
    --Do your statics here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have such SQL which select, group and order Url field from Statistic table.
I have Statistic table with these fields Id UserId DateStamp Data Also there is
I have an order-statistic augmented red black tree. it works for the most part.
I'm trying to time an order statistic function implemented in SBCL . Googling around
I have a big table with 10M rows. And I need to get some
I have the following function that draws some data from a chi-squared distribution and
Hello, Specs: VS 2010 C++ professional edition. I have a project with some statistical
I want to do some statistic analysis in R on a column with the
I see on sites that they sometimes have a statistic showing how many views
I am using jquery tabs within my CakePHP project. Here is my Script: $(function()

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.