Query criteria to replace records with multiple subtotals - ms-access

EDIT comments pointed out my question was too broad so I have done my best to focus it.
Trying to build a finance tracker in MS Access 2016 for spending analysis. I want to bring together multiple records from various bank accounts each one having a respective table that is identical to the bank statements (for ease of correcting). So lets say I have Bank1-T and Bank2-T. All bank account tables will have identical fields.
However I would also like to have the ability to, on occasion (where needed), replace certain records with subtotals to increase spending tracking granularity.
For example say I went to the gas station and paid for petrol as well
as lunch in the same payment, I would like Bank1-T to
have the single entry for payment to the petrol station, but to have
Accounts-T have the separate entries for payment of petrol and food.
Since I don't want to edit the bank account tables, I though I'd go ahead and store the split subtotals for payments in a table PayDetails-T. As there would only be a few payments I would store in PayDetails-T, how would I go about setting up a Query that brings together the payments from the various bank tables, and replaces the specific payment records from the bank tables with the ones in PayDetails-T? Can it be done in a single Query or would multiple be needed?

Related

Organizing monthly account extracts for personal use MS Access

I'm a bit of a newbie with databases and database design, but I'm hoping someone can point me in the right direction. I currently have 14 monthly loan extracts, each of which contain all accounts, their status, balance and customer contact info as-of month end. Not knowing what to do, I imported each of the monthly files into Access with each table acting more like a tab from an Excel workbook. Laugh away - I now know that's not how it's supposed to work.
I've done my homework and I understand how to split up part of my data into Customer and Account tables, but what do I do with the account balances? My thought is to create a Balances table, create a relationship to the Accounts table and create columns for each month. This seems logical, but is it the best way?
99% of my analysis involves trend reporting and other ad hoc tasks - tracking the total balances by product type over time given other criteria, such as credit score or age. My intended use is to create queries to select the data I need and connect to it via Get & Transform in Excel for final manipulation and report writing.
This also begs the question "how normalized should my new database be?" Each monthly extract is cumulative, so a good 75% of my data is redundant contact info already, but how normalized should I go?
Sorry for ranting,but if anyone has any experience in setting up their own historical database or could point me in a direction that will get me on track, I would appreciate it.
Best practice for transactional systems is close to what you expect:
1. Create a Customer table
2. Create an Account table
3. Create an Account Balance table
4. Create relationships from the Account to Customer, and from the Account Balance to the Account table.
You can create a column for each month, provided you have Year as part of the key of the Account Balance table. Even better would be to have the key for the Account Balance be Account ID and Date.
However, since you are performing analytics over the data, a de-normalized approach is not only acceptable -- it is preferable. So yes, you can (and perhaps should, based upon your use cases) put all the data into one big flat table and then compile your analytics.

Money expiration tracking

I am working with money expiration tracking problem at the moment (originally it is not money, but I have used it as a more convenient example).
An user can earn money from a platform for some mysterious reason and spent them for buying stuff (products, gifts etc.).
I am looking an algorithm (SQL query best case) to find a current balance of an user balance.
The events of spending and earning money are stored different database (MySQL) tables (let's say user_earned and user_spent). So in normal case, I would simply count user totals from user_earned and subtract spent money (total of user_spent).
BUT! There is a condition, that earned user money expires in 2 years if they are not used.
That means, if user have not used his money or used just a part of it, they will expire. If an user uses his money, they are used from the oldest not expired money record, so the balance (bonus) could be calculated in user's favor.
These are 5 scenarios with events in time, to have a better understanding on the case:
Both tables (user_earned and user_spent) have timestamps for date tracking.
I did something similar in one of my projects.
Looks like you need an additional table spends_covering with columns
spend_id, earhed_id, sum
So for each spends record you need to insert one or many rows into the spends_covering to mark 'used' money.
Then balance would be just sum of not used where date is less than 2 years.
select sum(sub.earned_sum-sub.spent_sum) as balance
from
(select e.sum as earned_sum, sum(sc.sum) as spent_sum
from earned e
left join spends_covering sc on e.earhed_id=sc.earhed_id
where e.date BETWEEN ...
group by e.earhed_id
having earned_sum > spent_sum) sub
It may be worth it to have two tables -- one (or more) with all the historical details, one with just the current balances for each 'user'. Be sure to use transactions to keep the two in sync.

Normalizing tables for payment on delivery of a single monthly invoice with entire balance due

I'm trying to wrap my head around the proper way to design tables for a billing policy where the customer can pay for each delivery as they are completed, or they can receive a monthly invoice with the entire balance due. This is a small bit of what I have already...
I'm not sure where to go... should I create a table for paid in full and monthly statement and relate them to the invoice table... or should I relate those to the payment table... or am I all over the place....
It's a lot more complicated than you think. You also have to account for partial payments, overpayments, payments on account, refunds and other real world complications.
The only way to approach this which will actually reflect the ugliness of the real world is to treat individual charges as one thing (your deliveries), summary documents (monthly invoice/statement) as another thing, and payments/refunds as another thing.
From there you need to join these three separate things together with relationships. The temptation is to join monthly invoices to payments, directly, but that would be a mistake. At least, it would make your life more complicated than it needs to be.
The relationship between charges and invoices is relatively easy. If your customer gets a monthly invoice, then all their packages from that month point at that invoice. This makes a many to one relationship from Package to Invoice.
The more complex part is the relationship between payments and charges. For this you need a new table Payment_Application which is a many-to-many intersection between charges (Packages) and Payments. This intersection entity will also have the amount of money paid for each package. This is important because you can record here whether the amount paid was too much, too little or the correct amount.
If you want to get more sophisticated, you could normalize the payment information so that you have a payment header and detail. This lets you keep information about a payment you receive separate from information about how you choose to apply that payment.
Consider the following:
Here you have an (optional) invoice header and an invoice detail (Package) as well as a payment header and an (optional) payment detail, which may or may not relate to an invoice detail. An arrangement like this will cover the various ugly things that happen when you need to track the exchange stuff for money.

Database Design: How to track airline ticket sales in Access Database?

I'm working on my final project for an intro to Access DB class. My assignment is a general "Create a DB with at least 3 tables, of which 2 are linked," with some specific requirements for types of tables (customer relations, financial management, product/service-related data).
I decided to create a database for a fictional airline. I need to track financial information, particularly the sale of tickets. I have a tblFlights table that includes a BaseFare field, a tblAirports table that includes airport fees and airport taxes and a tblInvoice table that currently only has invoice number and customer ID fields in it. I suppose that I would use a report to generate an actual invoice, not a table, right? So, I don't want to store a bunch of financial data in an invoice table, and maybe I shouldn't even have an invoice table? How do I keep track of how much money customers owe and how much they have paid?
I suppose that I would use a report to generate an actual invoice, not a table, right?
Yes, use a report.
So, I don't want to store a bunch of financial data in an invoice table, and maybe I shouldn't even have an invoice table? How do I keep track of how much money customers owe and how much they have paid?
Well, I suppose some industries might be able to get an airline to send them in invoice, but I've never heard of that myself. In my experience, you pay first, then they give you the ticket. So I'd expect a table of tickets, which would identify the flight, the seat (or its equivalent), the person's name, and something to indicate that it's paid (a Boolean flag, a price, something like that).

MySQL - Calculating Values vs Storing in Fields

I apologize if this has been asked before, but I'm pretty new to this and unable to find an answer that addresses the situation I'm faced with.
I'm trying to put together a database to run behind our company website. The database will store information on customer invoices and payments. I'm trying to figure out if I should create a field for the invoice balance, or if I should just have it calculate when the customer account is accessed? I don't want to create redundant data, and don't want to have the chance that somehow the field wouldn't be updated, and would thus be incorrect...but I also don't want to create to large of a burden on the server - especially if we pull up an overview of customer accounts - which would need to then calculate the balance of every account. Right now we are starting from scratch, so I want to set it up right!
We are anticipating having a couple hundred customer accounts by the end of the year, but will most likely be up to a couple thousand by the end of next year. (Average number of invoices per customer would be roughly 2-3 per year.)
There are probably other things to consider as well. For example, what if your invoice consist of ID's of products in another table... and the prices of those other products change? When you go to generate the invoice, you'll have the wrong total in there for what the guy actually paid 6 months ago. So if its a situation like that, you'll probably want to store the total on the invoice. And I wouldn't worry too much about doing a little math if you go the other route, it's not likely to be a huge bottleneck.
Yes, remember that items/goods could and will change their prices over time. You need to have the invoice balance as of the day of the purchase. Calculating the balance on the fly could lead to wrong balances later on.
Invoice balance is essential data to store, however I think you meant account balance since you referred to that later.
Storing the account balance would be denormalizing it, and that's not how accounting databases are typically designed. Always calculate account balance from invoices minus payments. Denormalizing is almost always a bad idea, and if you need to optimize in the future, there are other places to cache data that are more efficient than the database.
In your use case, a query like that on a few thousand rows would be negligible anyway, so don't optimize before you have to.