Creating a database structure where one column has multiple values? - sql

I have a SQL database that uses MS Access as a front-end and is coded with VB. Data is imported daily from an Excel spreadsheet in the form:
Journal Date, IO Account, Amount, Posted Date, Line Description, User ID, Date Checked, Voucher
The Voucher data is formatted in the Excel spreadsheet like:
1111111;2222222;3333333
The Voucher can have as many voucher numbers as needed (anywhere from 1 - 13 different num bers usually) and each number is separated by a semi-colon. Also, each number can differ in physical length. I then need to link these voucher numbers to multiple different tables. The data structure that was in place when I took over this project stored the voucher in the semi-colon separated form and parsed the string to link to other tables. I'm getting ready to revamp this database, and I don't want to store the data like this format because I know it violates 1NF.
I know I should probably use foreign keys, but I'm not sure how with the varying Voucher data. I have the ability to parse the data any way possible with VB in Access when it comes from Excel, I just don't know how to store it to relate my tables.
The goal of the project is to match everthing that relates to each distinct voucher and see if the amounts match up. Right now the process takes anywhere from 5 - 10 minutes to split the vouchers and compare the data across tables.
Let me know if you need more information. Any advice would be appreciated.
Thanks!
EDIT- Let me be more clear:
Here is an example voucher: 2988670;2990020;2989130;2991597;ONL112382
There are multiple entries in the table (call it T1) with this voucher with differing amounts. Each of the numeric values (excluding ONL number) correspond to another table (call it T2). If each of these 4 numbers are in T2, they are said to be "Matching". If they are matching and the sum of the amounts in T2 equal the sum of the amounts in T1, they are "Matching with no difference".
Sometimes the voucher looks like this: ED414596
In this case, I have to compare this value to a completely different table (call it T3). It includes the same matching process as above.
*Let's just say the voucher comes across in multiple ways and it needs to be compared to multiple tables.

Your voucher table would have a voucherID and a voucherNumber, this would allow voucherID to be repeated as many times as necessary. voucherID would be a foreign key from your first table, and the two columns together would be the PK for this table.

Related

SQL Best way to return data from one table along with mapped data from another table

I have the following problem.
I have a table Entries that contains 2 columns:
EntryID - unique identifier
Name - some name
I have another EntriesMapping table (many to many mapping table) that contains 2 columns :
EntryID that refers to the EntryID of the Entries table
PartID that refers to a PartID in a seprate Parts table.
I need to write a SP that will return all data from Entries table, but for each row in the Entries table I want to provide a list of all PartID's that are registered in the EntriesMapping table.
My question is how do I best approach the deisgn of the solution to this, given that the results of the SP would regularly be processed by an app so performance is quite important.
1.
Do I write a SP that will select multiple rows per entry - where if there are more than one PartID's registered for a given entry - I will return multiple rows each having the same EntryID and Name but different PartID's
OR
2.
Do I write a SP that will select 1 row per entry in the Entries table, and have a field that is a string/xml/json that contains all the different PartID's.
OR
3. There is some other solution that I am not thinking of?
Solution 1 seems to me to be the better way to go, but I will be passing lots of repeating data.
Solution 2 wont pass extra data, but the string/json/xml would need to be processed additionally, resuling in larger cpu time per item.
PS: I feel like this is quite a common problem to solve, but I was unable to find any resource that can provide common solutions or some pros/cons to different approaches.
I think you need simple JOIN:
SELECT e.EntryId, e.Name, em.PartId
FROM Entries e
JOIN EntriesMapping em ON e.EntryId = em.EntryId
This will return what you want, no need for stored procedure for that.

How to deal with one single cell containg multiple values?

I'm having an exercise requiring to create two table for a travel business:
Activity
Booking
it turns out that the column activities in the Booking table references from the Activities table. However it contains multiple value. How do I sort it out? If I insert multiple rows there will possibly duplication in the Booking's primary key.
As Gordon mentioned you should refactor your tables for better normalization. If I interpret your intent correctly this is more like what your schema should look like. Booking should only contain an ID for adventure and an ID for Customer. You will add a row to [AdventureActivity] for each activity booked on a [Booking]. With this design you can JOIN tables and get all the data you require without having to try to parse out multiple values in a column.

How to repeat records in access table number of times

I need your assistance to figure out how to achieve the following in MS access database.
I have a table with a lot of columns but one of them has a numeric value that will be used as how many times will the record will be repeated.
I need to make another table with repeated records based on Count column.
Build a numbers (aka tally) table (you can google it). I'll call it tblNumbers. Then all you need to do is create a query SELECT <yourTable>.* FROM <yourTable>, tblNumbers WHERE tblNumbers.Number <= <yourTable>.<numberField>

Using SQL databases I need to gather data for a client from multiple sources

I have multiple databases that contain pieces of data that I need to collect on my clients. Without giving any specific examples, due to confidentiality of the actual data, I will simply refer to what the field names are.
My Master table and three other tables contain the following columns -
Social Security Number,
Medicare Number,
Medicaid Number,
Phone Number,
Date of Birth,
Last Name,
First Name
The goal is to read a master record and, if all of the specified fields do not contain data, go and look at the other data sources to see if one of them DO contain the missing data.
Let me tell you an example of what the problem might be and see what suggestions you can give me to help me achieve my goal. In my example I will call the master table Table 1.
Table 1 - Is missing the DOB, SSN & Medicaid# for this record.
Table 2 - Contains the DOB, Medicare# and Last & First Name.
Table 3 - Has DOB, Medicaid#, Phone Number and Last & First Name.
Table 4 - DOB, SSN, Medicare#, and Phone Number, and Last & First Name.
Currently, I am doing the following:
I created a view called View 1 to combine all of the tables together. The uncommon fields are simply NULL for the tables no containing the field.
I have nested case statements for each of the desired fields. I look to see if the field in Table 1 is NULL, I begin doing a SubSelect statement to look for a matching record in the View 1 for each of the possible matching fields along with any secondary field to double check when needed - like if I do just DOB and Last & First Name matches.
I have a temporary table that gets updated with the findings prior to me running through the checks again. I run through it multiple times since the first time through it might not have had a hit with one field, but the second time through it would find a match.
Does anyone see a better way of doing it thn what I have described?
This is the part that loses me:
I have a temporary table that gets updated with the findings prior to
me running through the checks again. I run through it multiple times
since the first time through it might not have had a hit with one
field, but the second time through it would find a match.
Without that I would suggest left joining the tables to the master then using COALESCE() to find your best NOT NULL value.
COALESCE(Table_1.DOB,Table_2.DOB,Table_3.DOB,Table_4.DOB)

What is the proper way to store an array into a database table?

I have an array of 50+ elements that dictates how many hours were worked for a given week.
What is the proper way to store this information into a database table?
My initial idea was to use a delimiter, but the text is too large (280 characters) to fit.
Additionally, there seems something "wrong" with creating a table column for each element.
Ideas?
Array using delimiter (comma):
37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5,37.5, ...
The "proper" way is to store the array's contents as multiple rows in a whole other table, each with a foreign key referencing the record they belong to back in the first table. There may be other things that work for you, though.
[EDIT]: From the details you added I'm guessing your array elements consist of a number of hours worked each week and you have 50+ of them because a year has 52-ish weeks. So what I think you're looking for, is I guess that your current (main) table is called something like "employees," is that each row there should have some unique identifier for each employee record. So your new table might be called "work_weeks" and consist of something like employee_id (which matches the employee id in the current table), week_number, and hours_worked.
Seems like a 1 to many relationship. For this example, tableA is the 1 and tableBlammo is the many.
tableA => column blammoId
tableBlammo => column blammoId, column data
One row in tableA joins to multiple rows in tableBlammo via the blammoId column.
Each row in tableBlammo has one element of the array in the data column.