Jump to content

Recommended Posts

I have several hundred thousand records that will add a monthly db row if new data exists. I plan on working this info into a chart, but I see no reason to add values that contain 0. For 350,000 records, doing it this way reduces new monthly records to about 8,500, vs 350,000 - a huge performance improvement if this is possible.

 

I figured that an array will help solve this.

 

For example, looping over the data in SQL, will return something like:

 

while ($r = $result->fetch_assoc()){
$id[] = $r["id"];
$revenue[] = $r["revenue"]; // Revenue for example
$date[] = $r["date"]; // 2016-07
}
 
So, the result will have the dates, id's and revenue stored as an array. Using ksort() will sort the dates, however is there a plausible way to add missing dates (that have 0 values)?
 
Sorry for not being more clear, this is the best way I could describe this. I thank you for your help. DB storage is limited and expensive, and this is a critical step on the long term project.
Link to comment
https://forums.phpfreaks.com/topic/303081-planning-stage-filling-in-blank-data/
Share on other sites

 I for one dont understand what your doing. Forget your attempted solution for a minute. Try to describe better what you have and what it needs to do.

 

I have several hundred thousand records that will add a monthly db row if new data exists.

 

This reads like you have all those records that will add a row, as in a single row. That doesn't make sense at the moment. Where is the hundred thousand records coming from?  Best I can tell is you have three columns, an id, revenue, and date. If the dates have a zero value what date are you going to replace it with? Why do some have a zero for a date in the first place?

Edited by benanamen

Hi Benanamen,

 

I need this data to use with highCharts.js, and will plot the data by month. So for example:

2016-05 Revenue: $20

2016-08 Revenue: $10

2016-11 Revenue: $50

 

How can I populate the empty data where no month exists in between?

To chart this out in a way that makes sense, I need this:

 

2016-05 Revenue: $20

2016-06 Revenue: $0

2016-07 Revenue: $0

2016-08 Revenue: $10

2016-09 Revenue: $0

2016-10 Revenue: $0

2016-11 Revenue: $50

2016-12 Revenue: $0 // This month, or months after last pulled record up until now

 

The records are coming from traffic to a website. There are 350,000 original database rows for traffic sources. I am trying to collect archived info on a monthly level.

 

I hope that this clarifies my original post.

2016-05 Revenue: $20

Edited by iwpg

That is very much clearer and easily understandable. Unfortunately I am done for the night so I can't help you any further right now. I am sure someone will probably give you direction before I can get back to you.

 

In the meantime, get familiar with the XY Problem in my signature. It will help you and us in the future.

Edited by benanamen

When I need to extract chart data covering a range of dates I find it easier to create a temporary table containing all the dates that I need to show on the chart. I use a DatePeriod object to create the range of dates EG

mysql> select * from tempdate;
+---------+
| month   |
+---------+
| 2016-01 |
| 2016-02 |
| 2016-03 |
| 2016-04 |
| 2016-05 |
| 2016-06 |
| 2016-07 |
| 2016-08 |
| 2016-09 |
| 2016-10 |
| 2016-11 |
| 2016-12 |
+---------+

So if I have a revenue table like yours ...

mysql> select * from revenue;
+---------+---------+
| month   | revenue |
+---------+---------+
| 2016-05 |      20 |
| 2016-08 |      10 |
| 2016-11 |      50 |
+---------+---------+

... I can fill in the gaps with

SELECT t.month
  , IFNULL(r.revenue, 0) as revenue
FROM tempdate t 
LEFT JOIN revenue r USING (month);

+---------+---------+
| month   | revenue |
+---------+---------+
| 2016-01 |       0 |
| 2016-02 |       0 |
| 2016-03 |       0 |
| 2016-04 |       0 |
| 2016-05 |      20 |
| 2016-06 |       0 |
| 2016-07 |       0 |
| 2016-08 |      10 |
| 2016-09 |       0 |
| 2016-10 |       0 |
| 2016-11 |      50 |
| 2016-12 |       0 |
+---------+---------+
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.