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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:29:38+00:00 2026-05-29T20:29:38+00:00

!Modified! I have a table (mySQL 5.0.x) which contains values for date ranges. |

  • 0

!Modified!

I have a table (mySQL 5.0.x) which contains values for date ranges.

| id | link_id | type_id | value |  start | end    |
====================================================
|  1 |       1 |       1 |    10 | 201111 | 201202 |
|  2 |       1 |       2 |    20 | 201110 | 201201 |
|  3 |       1 |       1 |   100 | 201202 | 201202 |
|  4 |       2 |       1 |    40 | 201202 | 201203 |

where

  • id is the id for record
  • link_id is for linking the data to other tables
  • type_id is for determining the type of the value from other table
  • value is numerical value
  • start and end define the range (real date or integer for year and month)

The trick is that I need to present total value per type and link for a given period on a monthly basis. So the result should be like this for the range 201201 – 201202:

| period | link_id | type_id | value |
======================================
| 201201 |       1 |       1 |    10 |
| 201202 |       1 |       1 |   110 |
| 201201 |       1 |       2 |    20 |
| 201201 |       2 |    NULL |  NULL |
| 201202 |       2 |       1 |    40 |

I could use PHP to put the information like this into the database but there are some drawbacks. I have hundreds of link_ids, many type_id, and the average difference between start and end dates is 30 months so I would have tons of rows.

  • 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-29T20:29:40+00:00Added an answer on May 29, 2026 at 8:29 pm

    Here’s one way to do it. I’ve put in $startMonth and $endMonth so you can see how they play in. (I have this niggling feeling this can be simplified, but it currenty escapes me):

    SET @MONTH:=$startMonth-1;
    
    SELECT month, link_id, type_id, SUM(value) AS value
    FROM (SELECT @MONTH:=@MONTH+1 as month
          FROM foo f
          LIMIT $endMonth-$startMonth+1) a
    LEFT JOIN foo f
     ON a.month >= f.start AND a.month <= f.end
    GROUP BY link_id, type_id, a.month;
    

    The way it works: suppose you had a table a:

    +-------+
    | month |
    +-------+
    |201201 |
    |201202 |
    +-------+
    

    Then the query you’re after is a lot simpler to work out:

    SELECT month, link_id, type_id, SUM(value) AS value
    FROM a
    LEFT JOIN foo
     ON a.month >= f.start AND a.month <= f.end
    GROUP BY link_id, type_id, a.month;
    

    Ie you join table a to foo by making sure the month is between the start and end periods, and then you just sum up value, grouping by month, link id, and type id.

    So the problem is how to generate table a which contains numbers from $startMonth to $endMonth. Hence the:

    SET @MONTH:=201200;
    SELECT @MONTH:=@MONTH+1 AS month
    FROM foo
    LIMIT 2;
    

    This gives a table of numbers startign from 201201 and with 2 rows, ie up to 201202. In this query the FROM foo is dummy – not really used.

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

Sidebar

Related Questions

I have a MySQL table that contains many rows. The table structure is as
I want to find out which tables have been modified in the last hour
I have a table in MySQL that has a primary key: mysql> desc gifts;
I have a database table in mysql with a field that is of TIMESTAMP
Im trying to insert data into a table in MySQL. I found/modified some code
In MySQL, I'm sick of adding the columns dt_created and dt_modified (which are date
please i have a modified_by column in mysql table with string like this 40,1280825613|40,1280825617
I'm doing a mysql table and i have one column that has current timestamp
I have a mysql database and the application which connects to it, dumps required
I have a post model (mysql table), and the structure is something like: id,

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.