Laravel array filtering - mysql

Developers, I am working on a report that I have to display the companies ranking based on some conditions for the date range which comes from front. Ex: last week, I have done that, but now I want to show how many weeks that a particular company be in same rank. If I am checking the past week I have to check the ranking for each week from the year start. If last week first position company and other weeks first position company is same I have make the count as increasing accordingly. When I querying the data for each week using the for loop it is taking around 42s to process and display the data. Also I tried to fetch whole data from first week of the year to current week then I filtered the array but this also takes long time. Can anyone give any other ideas to overcome this? Thanks in advance.

As far as I understand your problem - then storage of aggregated data should help you.
Create a table in the database, let's say "archive_rating", with 3 fields: week_number (let from January 1, 2000), company_id, company_position in your rating. Don't forget the index for the week_number field.
At 00:00:00 every Monday, run a background task that will save the positions for each company to an archive table.
This will allow you not to calculate a rating for each week from the beginning of the year. You will already have it.

Related

Database structure for storing schedules/cron job?

I am stuck with a problem. In an app's db, I am having a schedule table which will store user provided schedules. E.g
Daily
Every Week
Twice a Week
Every 3rd (or any user chosen) day of week
Every Month
Twice a month
Every x day of month
Every x month of year
And so on. These schedules will then provide reference point to schedule different tasks or identify their repeat-ance.
I am not able to think of a proper database structure for it. The best I can get is to have a table with following columns:
Day
Week
Month
Year
type
Then store the specified schedule in the related column and provide the type.
e.g Every week can go like 1 in week column and 1 (designated value for repeating whole) or something like that.
The problem with this approach is that this table is gonna be used very frequently and the data retrieved will not be straightforward. It will need calculation to know the schedule type and hence will require complex db queries to get each type of schedule.
I am implementing it in Laravel app if that can provide any other methodology. It's a SAAS app with huge amount of data related to the schedule table.
Any help will be very much appreciated. Thanks
I suggest you are approaching the problem backwards.
Devise several rules. Code the rules in your app, not in SQL. When inserting an event, pre-fill a calendar through the next 12 months with all occurrences of the event. Every month, go through all events and extend the "pre-fill" through another month (13 months hence).
Now the SELECTs are simple and fast.
SELECT ... WHERE date = '...'
has all the events for that day (assuming it is within 12 months).
The complexity is on inserting. But presumably you insert less often than you select.
The table with the event definitions would be only as complex as needed for your app to figure out what to do. Perhaps
start_date DATE,
frequency ENUM('day', 'week', 'month', ...)
multiplier TINYINT, -- this lets you say "every second week"
offset TINYINT, -- to get "15th of every month"
Twice a week would be two entries.
Better yet, there are several packages (in Perl, shell, etc) that provide a very rich language for expressing event-date-patterns. Furthermore, you may be able to simply 'call' it to do all the work for you!

Avg difference between consecutive mysql rows for specific month

I'm busy creating a personal mobile web app for home management.
Each day a READING is entered into a table, along with the days DATE.
One reading per day, meaning DATE is unique. READING could be the same, by very unlikely, if there is no usage for that day.
A usage amount for the day is calculated, by subtracting the previous days reading from the newly input reading.
How would I calculate the average usage numbers for a particular month?
Should the usage amount, once calculated, perhaps be stored back to the newly added row? Leaving for easy use of this to find an average?
Should a separate primary key be added, numbering the records, as apposed to using date to calculate the latest record added?
Thank you in advance, any help appreciated
Final solution:
When adding the latest readings, calculate day usage using previous record (found by using date). Add this to a third column.
To find daily average for current month, use MySQL avg, and limit to current year, and current month.

MS-Access: updating query for each new linked table each week

First - thanks for your time. My issue lies in my new usage of Access for tracking values from weekly excel reports. Each week I'm given a new excel file with updated values for about 50 employees. These values generally track their performance over 6 different metrics. I've begun to link these excels into an access database to keep and track that data each week. These linked tables are given the name convention of the date that the data is as of - example: 05-05; 05-12; 05-19, 05-26; etc.
My question is - is there a way to build a query to track the change (difference in values) from last week to this week (05-19 to 05-26), automatically? And also taking into account future additions of linked tables so that I don't have to add a piece to the query each week?
In addition, I'm looking to track overall change - first table 05-05 to the most recent linked table (which ever date that's true for, whether it's the end of July, or the end of the year).
Based on these 2 results, I'd eventually build out the query to show every week with their value and in the next column the week over week change (up down or neutral)

How do I get a table that shows events per user over a time interval?

This is very hard to explain in just one question, however here goes.
In MySQL database I have a table called video that has all the videos created.
The structure is something like this
id
owner
title
created_at
I have another table user, something like this
id
name
created_at
The goal is to get the following graph
x axis is the month #
y axis is the # of videos per month
As you can see, it shows how many videos each user created each month. BUT the first month is always 30 days from the date the user is created. So for user Ben it is going to be from 2016-10-01 to 2016-11-01 but for Rick it is from 2016-08-01 to 2016-09-01
So for Ben's first month it is different from the first month of Rick by approximately 60 days.
But in the graph I want to show all of them as month #1
My solution for now is to "normalize" the data. So to get the data for Ben I took the difference between a fixed date (I set it to 2016-01-01) and calculated the difference of days.
Then I subtract the number of days for all the videos and this way all the videos first month is going to be 2016-01-01 to 2016-02-01
This works, but I'm really not sure if this is the best approach.
Anyone with a better idea of how to do this?
Thanks
I was told about a better way to do it.
Instead of normalizing the dates as I was doing, we just create a new property
I updated my create_video event to have a property like age_of_user. Then I can group_by age_of_user to see how many videos they uploaded in whatever number month.
Something like
{
age_of_user: {
days: 64,
weeks: 9
months: 2,
}
}
Each event is created with this new property.
And when I query I group by month and by user and get all the data I need
This way I don't need to mess up with the timestamp of the video and I can use it in different queries.

sql query count daily difference / count new rows, no date column

I have a database for our local real estate listings, there are no dates or timestamp columns.
I would like to be able to get out just the rows that were added in the past day or two.
Can anyone point me in the right direction to get this data out?
EDIT:
Each new row does get a new id number which is incrementally higher, so I can ORDER the results by newest.
Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?
"Would it be possible to save my query count in a file, or in another database, each day, then calculate the difference and use that as my number of new listings?"
I understand that you can't change the table structure to add a date...so instead, I suggest to have a cron job at midnight that will create a record with a date and the higher ID at this moment. This way, you will be able to finde a range of ID for a specific date...
If you relayed on query count, you will get problem when you will start to delete some rows...