I have a sort of simple ledger in my app. I keep track of each balance adjustment i.e. deposit $10 into an account with $5 creates a new row containing the amount of $10 and the total of $15. The $15 number is the part I'm having a hard time figuring out how I should handle it.
I can do it totally app code side with my DB adapter by getting the last row's total and adding to the new amount and creating a new total_balance based on that. This, however, feels wrong to me for some reason. I feel like I should be able to do this logic all in MySQL somehow.
Something like "create a new record WHERE user_id='123' and the amount is +10" and it'd make a new record with 15 for the total_balance automatically.
Is this possible?
Related
User wants to see a report of when resources are NOT being used during the work week on average.
Available to me I have a reservations table along these lines:
table
Attached is the picture of how the user would like the data represented. example graph
I think my issue lays in getting the data in the right format, how can I take a reservations table and inverse it so to show all available time in-between two reservations.
I was able to get a Bar graph that showed the average availability of the days that the resource was booked, but it didn't consider days that there was no reservation information, it also did not account for situations where the user had booked for more then one day.
Have you tried making a calculated column?
This worked for me:
1440 - Sum([Duration]) OVER ([StartDateTime])
1440 is the amount of minutes in a day. It subtracts the total amount of 'busy' time in each day from the total minutes in the day.
It is then easy to make a bar chart from this column.
This worked in my testing, but it might run into issues if you put it into practice. Keep me updated!
Table with calculated column, and barchart.
I need to maintain everyday closing balance of a customer and plot a line graph based on that balance for the last 365 days. Which data model is preferred to maintain this data ?
MySQL, Cassandra or any other databases ?
The obvious solution would be to have a table with a key [client id, data] and the value being closing balance.
The question is how do you fill that data in? You could have a running job at the day end. The big question is how to make the system reliable? If job fails and is restarted next day, will that provide data for the previous day?
Typical way of addressing this type of problem is using "event sourcing". This is a fancy way of saying that it needs to be a storage of records for every operation executed on a balance. Every add/deduct should be there, including client id and date. These records also may have "resulting balance" - which implies that last operation for a customer in a day has the closing balance as well.
At the end, you will have list of transactions and you will be able to "replay" previous event to get balances. It's your choice if you want to have actual table for daily balances per client - as it may be cheaper to look up that data instead of recalculating it every time.
In banking industry, every transaction is stored as a separate record for this specific reason - to be able to get different reports; incl. closing balances per day.
We are building a warehouse stock management system and have a stock movements table that records stock into, through and out of the system, for each product and each location it is stored. i.e.
10 units of Product A is received into Location A
10 units Product A are moved to Location B and removed from Location A.
1 unit is removed (sold) from Location B
... and so on.
This means that over to work out how much of each product is stored in each location we would;
"SELECT SUM('qty') FROM stock_movements GROUP BY location, product"
(we actually use Eloquent but I have used SQL for an example)
Over time, this will mean our stock movements table will grow to millions of rows and I am wondering the way to best manage this. The options I can think of:
Sum the rows as grouped above and accept it may get slow over time. Im not sure how many rows it will take before it actually starts to cause any performance issues. When requesting a whole inventory log via our API each row would have to be summed for every product, so this will compile to a fairly large calculation.
Create a snapshot of the summed rows every day/week/month etc. on a cron and then just add the sum of the most recent rows on the fly.
Create a separate table with a live stock level which is added to and subtracted with every stock movement. The stock movements table shows an entire history of all movements while the new table just shows the live amounts. We would use database transactions here to ensure they keep in sync.
Is there a defined and best practice way to handle this kind of thing already? Would love to hear your thoughts!
The good news is that your system is already where a lot of people say the database world should be moving: event sourcing. ES just stores every event against an object, in this case your location, and in order to get the current state you have to start with an empty object and replay all of that objects events.
Of course, this can be time-consuming, and your last two bullet points are the standard ways of dealing with it. First, you can create regular snapshots with the current-as-of-then totals for that location, and then when someone asks for the current-as-of-now totals you only need to replay events since the last snapshot. Second, you can have a separate table of current values, and whenever you insert a record into your event store you also update the current value. If they ever get out-of-sync, you can always start fresh and replay the entire event series again.
Both of these scenarios are typically managed through an intermediary queue service, like SQL's Service Broker, RabbitMQ, or Amazon's SQS: instead of inserting an event directly into your event store, you send the change into a queue and the code that processes the queue will update your snapshot.
Good luck!
I am in the process of designing a very simple system for keeping track of a shifts worked by the sales team. Although i'm still designing and not done yet, I later realized I left out one important detail that I'll get into.
Some quick background first:
Each shift consists of how long the shift was (hours worked), whether it was the morning, afternoon, or night shift (or some combination), how much in sales were made for that shift, and how many tips the person made that day. I also keep track of things like whether it was cash or credit tips you received for doing your job well.
So one extra thing that happens is the sales team gets paid by the hour. For reports and running totals I am currently just calculating your income for that shift as pay rate (from job Position table) multiplied by the hours you worked, but I forgot the fact they could get raises! This means that if you get a raise, your income would magically increase (historically speaking)! I might've been making $10 an hour last year, and now $15 an hour this year - So the reports would be wrong.
My initial thought was to simply add a new column in my 'shift' table called HourlyRate, and that would be whatever the current hourly rate was at that moment in time. $10, $15, etc. Then I can just use this rate when i'm performing calculations
Do you all think this is an acceptable solution? Would it be weird to have a few hundred rows of sales all with the exact same hourly rate column for the same sales worker?
Thanks so much for your help!
Here is what I want to do. I am setting up a new commerce website for myself using WooCommerce. From each sale I will be donating $5 to a charity. In the header of my Wordpress site I want to have a counter that shows how much money we have donated in this current week.
So how do I get the required number from the WP database? I guess I need to some how extract the number of sales from the database, multiply that by 5 and put it in the header.php with a $ infront of it.
I don't mind if I have to change code at the beginning of each week to reset the counter.
Simple solution store a value of number of sales, create a wordpress option and increment it in the process of making the sale.
Otherwise you need to look more into the db structure of your ecommerce code and perform a count query of all records between today - 7d and today.