mysql trying to import an odd format csv - mysql

I have an excel CSV file where the customer was recording invoices. The made a new column for each vendor and put the amounts under each column.
Like this:
ABC Company Jacks Garage XYZ Company
123.45 223.22 123.11
423.11 10.22 11.21
Etc. I am trying to guess how to get that into two columns (Vendor, Amount) so I can import that data into the actual table. There are about 200 vendors so doing this manually cut and paste would work be take forever.
Can I do this with a loop somehow and insert the info into the 2 column table?

I would do this by writing a simple script written in just about any language, e.g. Python, PHP, Ruby, or even Perl. Any of those languages make it easy to read a text file, split the fields into an array, and post the fields into a database in whatever manner you want.
Alternatively, you could do this without writing code, but in the following steps:
Load the CSV file as-is into a table.
create table invoices_asis (
rownum serial primary key,
abc_company_amount numeric(9,2),
jacks_garage_amount numeric(9,2),
xyz_company_amount numeric(9,2)
);
load data infile 'invoices.csv' into table invoices_asis ignore 1 lines
(abc_company_amount, jacks_garage_amount, xyz_company_amount);
Then copy all data for each given vendor to your (vendor, amount) table.
create table invoices (
invoice_id serial primary key,
vendor varchar(20),
amount numeric(9,2)
);
insert into invoices (vendor, amount)
select 'ABC Company', abc_company_amount from invoices_asis;
insert into invoices (vendor, amount)
select 'Jacks Garage', jacks_garage_amount from invoices_asis;
insert into invoices (vendor, amount)
select 'XYZ Company', xyz_commpany_amount from invoices_asis;
Finally, drop the as-is table.
drop table invoices_asis;

I think what you want is to 'unpivot', for which there re many options (eg for Excel).
Insert a blank column at the extreme left then include that with your data at 4. in the example. At 6. move Column into ROWS above Row. Double-click the Grand Total and remove left-hand column.

Related

inserting multiple values into one row mySQL

How can i insert multiple values into one row?
My query
insert into table_RekamMedis values ('RM001', '1999-05-01', 'D01', 'Dr Zurmaini', 'S11', 'Tropicana', 'B01', 'Sulfa', '3dd1');
i cant insert two values into one row. is there another way to do it?
I'm ignorant of the human language you use, so this is a guess.
You have two entities in your system. One is dokter, the other is script (prescription). Your requirement is to store zero or more scripts for each dokter. That is, the relationship between your entities is one-to-many.
In a relational database management system (SQL system) you do that with two tables, one per entity. Your dokter table will contain a unique identifier for each doctor, and the doctor's descriptive attributes.
CREATE TABLE dokter(
dokter_id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL,
nama VARCHAR (100),
kode VARCHAR(10),
/* others ... */
);
And you'll have a second table for script
CREATE TABLE script (
script_id BIGINT AUTO_INCREMENT PRIMARY KEY NOT NULL,
dokter_id BIGINT NOT NULL,
kode VARCHAR(10),
nama VARCHAR(100),
dosis VARCHAR(100),
/* others ... */
);
Then, when a doctor writes two prescriptions, you insert one row in dokter and two rows in script. You make the relationship between script and dokter by putting the correct dokter_id into each script row.
Then you can retrieve this information with a query like this:
SELECT dokter.dokter_id, dokter.nama, dokter.kode,
script.script_id, script.kode, script.nama, script.dosis
FROM dokter
LEFT JOIN script ON dokter.dokter_id = script.dokter_id
Study up on entity-relationship data design. It's worth your time to learn and will enhance your career immeasurably.
You can't store multiple values in a single field but there are various options to achieve what you're looking for.
If you know that a given field can only have a set number of values then it might make sense to simply create multiple columns to hold these values. In your case, perhaps Nama obat only ever has 2 different values so you could break out that column into two columns: Nama obat primary and Nama obat secondary.
But if a given field could have any amount of values, then it would likely make sense to create a table to hold those values so that it looks something like:
NoRM
NamaObat
RM001
Sulfa
RM001
Anymiem
RM001
ABC
RM002
XYZ
And then you can combine that with your original table with a simple join:
SELECT * FROM table_RekamMedis JOIN table_NamaObat ON table_RekamMedis.NoRM = table_NamaObat.NoRM
The above takes care of storing the data. If you then want to query the data such that the results are presented in the way you laid out in your question, you could combine the multiple NamaObat fields into a single field using GROUP_CONCAT which could look something like:
SELECT GROUP_CONCAT(NamaObat SEPARATOR '\n')
...
GROUP BY NoRM

Import from Excel to SQL with conditional check for duplicates

I have a huge number of data stored in PDF files which I would like to convert into a SQL database. I can extract the tables from the PDF files with some online tools. I also know how to import this into MySQL. BUT:
The list contains users with names, birth dates and some other properties. A user may exist in other PDF files too. So when I'm about to convert the next file into Excel and import it to MySQL, I want to check if that user already exists in my table. And this should be done based on several properties - we may have the same user name, but with different date of birth, that can be a new record. But if all the selected properties match then that specific user would be a duplicate and shouldn't be imported.
I guess this is something I can do with a copy from temporary table but not sure what the selection should be. Let's say user name is stored in column A, date of birth in column B and city in column C. What would be the right script to verify these in the existing table and skip copy if all three match with an existing record?
Thanks!
1- Create a permanent table
Create table UploadData
(
id int not null AUTO_INCREMENT,
name varchar(50),
dob datetime,
city varchar(30)
)
2- Import your data in Excel to your SQL DB. This is how you do it in Sql Server mentioned below, not sure about MySQL but might be something similar. You said you know how to do it already in your question, that's why I am not specifying each step for MySQL
Right-click to your DB, go to Tasks -> Import Data, From: Microsoft Excel, To: Your DB name, Select UploadData table, (check Edit Columns to make sure the columns are matching), finish uploading from Excel to your SQL DB.
3- Check if data exists in your main table, if not, add.
CREATE TEMPORARY TABLE #matchingData (id int, name varchar(50), dob datetime, city (varchar(30))
INSERT INTO #matchingData
select u.id, u.name, u.dob, u.city
from main_table m
inner join UploadData u on u.name = m=name
and u.dob = m.dob
and u.city = m.city
insert into main_table (name, dob, city)
select name, dob, city
from UploadData
where id not in (select id from #matchingData)
4- No need UploadData table anymore. So: DROP TABLE UploadData
Add primary key constraints to Column A, Column B and Column C
It will avoid duplicate rows but can have duplicate values under single column.
Note: There is a limit on maximum number of primary keys in a particular table.

MySQL join on substring

I have a long list of strings in a column in various formats. I would like to search for that substring and then organize that column while leaving the original column.
There is one table with 8 columns, one of which contains strings. There is also a list of words in another table. I would like to match those the words in the list with those string as an additional column. This would be so in the large table of "Tom","tom","tom az", "Tom Hanks", would have another column that just has "Tom" (the corresponding match in the list) for all of them.
I have thought about using the JOIN function, but wasn't sure how to do that with substrings.
The excel logic would be something along the lines of "=IF((SEARCH("Tom","asfj Tom dsf")),"
TOM","NULL").
If there is more than one match is found, I would like to use the one with earlier 'ID' ( a column of integers from 1 - 11mil).
The question is still a bit ambiguous. You should really show the table structure, sample data and expected results.
Supposing you have this minimal structure and data:
CREATE TABLE People (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
FullName TEXT
);
INSERT INTO People (FullName) VALUES ('Tom Hanks');
INSERT INTO People (FullName) VALUES ('Mary 467');
INSERT INTO People (FullName) VALUES ('ann margaret' );
INSERT INTO People (FullName) VALUES ('ziddy tom');
INSERT INTO People (FullName) VALUES ('bloody mary');
INSERT INTO People (FullName) VALUES ('mary ann');
CREATE TABLE Names (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
Name TEXT
);
INSERT INTO Names (Name) VALUES ('Tom' );
INSERT INTO Names (Name) VALUES ('Mary');
INSERT INTO Names (Name) VALUES ('Ann' );
... then you can get a first result with a query like this:
SELECT p.*, n.Name
FROM People p
JOIN Names n ON p.FullName LIKE CONCAT('%',LOWER(n.Name),'%');
(tested on SQLFiddle here)
There are some problems though.
It's not clear if you want to search whole word only. Your Excel sample does not do that, so I stuck with a simple LIKE. To search for whole words the LIKE will not work properly.
"mary ann" comes up twice because that FullName contains two of the Names. You said you'd like to choose one depending on ID, but it's not clear if you understood the problem.

MYSQL Copying One Table to Another Table With Conversion of Columns

I'm copying data from one database table to another database table. This essentially is copying the data from our old format to our new format. So, in addition to simply copying columns value-for-value, I also need to do some conversions in the copy statements as well.
For, example, here is what I have to do the copying...
INSERT INTO new_database.table1 (id, product, is_default)
SELECT id, product, is_default FROM old_database.table1
The id and the product are working fine. But, in this example, the old_database stored "is_default" as a VARCHAR(1), either 'Y' or 'N'. The new_database stores "is_default" as a BOOLEAN.
How can I do the conversion between formats within the INSERT SELECT statement I'm already using?
Try like below:
INSERT INTO new_database.table1 (id, product, is_default)
SELECT id, product, IF(is_default='Y',1,0) as isdefault FROM old_database.table1

inserting data into a table that has a many to many relationship

I am working on an assignment and need your help with the following in SQL database:-
I have 3 tables
Product
LintItem
Invoice
LineItem is a bride table and I need to insert data into LineItem but it requires ProductID and InvoiceNumber.
In my case the Invoice table is emppty and it will be filled from the data that LineItem table passes.
The problem is how can I create an invoice before having the data from the lineItem table?
I am using these table for online shopping cart.
It's really hard for me to explain this problem. Hope you understand it, Thanks!
It sounds like you have a foreign key constraint forcing the existence of a Invoice record prior to inserting your line item records. It is hard to say exactly, based on the phrasing of your question but could be something like.
--Table variable to hold line items
DECLARE #lineItems TABLE
(
InvoiceNumber INT,
Quantity INT
)
INSERT INTO #lineitems VALUES(1,1)
INSERT INTO #lineitems VALUES(1,2)
--ADD INVOICE RECORD FIRST AND SUM Quantities etc....
INSERT INTO Invoice
SELECT InvoiceNumber,SUM(Quantity)
FROM #lineItems
GROUP BY InvoiceNumber
--NOW YOU CAN ADD LINE ITEMS
INSERT INTO LineItems SELECT * FROM #lineItems
This is a pattern you could use if that was your goal.
If you are wanting to insert these LineItems on the fly as the user is clicking Add from the webpage. I wouldn't use your LineItem SQL table for caching this way. Without knowing anything about your application it is hard to say but you really should be caching this data in the HTTP session or in the client as (array,json, local storage etc..). If you were to choose to do this as an SQL table just make a new LineItem without the constraints and then similarly per above you can use that table to insert into your LineItem table.