I want to add two columns in MySql workbench? - mysql

My table name is 'bills' i want the sum of columns Electricity and gas and their sum should automatically stored in the column named as Total.Which trigger can do this work please tell me its correct code.I have searched a lot but didn't got my answer.
Electricity+Gas=Total
1 2 3

Sorry, if HerrSekurr is correct and this is homework, it's a trick question.
Tell your teacher: "Teacher, one should never store a derived result back into a table. The beauty and power in SQL is the ability to generate the desired query with the CURRENT data."
To do so: "select electricity, gas, sum(electricity + gas) from your_table"
Now, if you want an index (maybe that's the database term you really meant), accept my answer, then ask a new question...

Related

Google AppsScript to create multiple forms with grid questions based on values of 2 arrays

I'm working on a combination of spreadsheets and forms to manage vendors' orders.
Column A is the vendors' name, column B is the orders, which looks like this:
VendorA - P0001
VendorA - P0003
VendorB - P0002
VendorC - P0004
VendorC - P0005
I want to create multiple forms in which have only a grid question with fixed column values and dynamic row values based on the order list of each Vendor. The order list will be updated from time to time so the row values should be able to automatically update as well (without me running the script again for every updates).
I've found sample scipts to create forms and edit questions, but the my final desired result is abit specific that I don't think just copying them works.
I'm relying on this tutorial right now: https://www.youtube.com/watch?v=k0RS4oPzy80 ====> I'm stuck at filtering the orders based on vendor names, as how to put it in the map() function.
I hate to ask someone else to do my homework but I have zero experience in this subject and my 48-hours search didn't help much since I didn't know the keyword to search for case studies. I wanna learn as well, therefore it's best if you guys can take me through the steps, not just throw the ball at me.
Your effort will be eternally appreciated. My best regards in advance.

SQL - Finding rows with unknown, but slightly similar, values?

I am trying to write a query that will return similar rows regarding the "Name" column.
My issue is that within my SQL database , there are the following examples:
NAME DOB
Doe, John 1990-01-01
Doe, John A 1990-01-01
I would like a query that returns similar, but not exact, duplicates of the "Name" column. Since I do not know exactly which patients this occurs for, I cannot just query for "Doe, John%".
I have written this query using MySQL Workbench:
SELECT
Name, DOB, id, COUNT(*)
FROM
Table
GROUP BY
DOB
HAVING
COUNT(*) > 1 ;
However, this results in an undesirable amount of results which Name is not similar at all. Is there any way I can narrow down my results to include only similar (but not exact duplicate!) Name? It seems impossible, since I do not know exactly which rows have similar Name, but I figured I'd ask some experts.
To be clear, this is not a duplicate of the other question posted, since I do not know the content of the two(or more) strings whereas that poster seemed to have known some content. Ideally, I would like to have the query limit results to rows with the first 3 or 4 characters being the same in the "Name" column.
But again, I do not know the content of the strings in question. Hope this helps clarify my issue.
What I intend on doing with these results is manually auditing the rest of the information in each of the duplicate rows (over 90 other columns per row may or may not have abstract information in them that must be accurate) and then deleting the unneeded row.
I would just like to get the most concise and accurate list I can to go through, so I don't have to scroll through over 10,000 rows looking for similar names.
For the record, I do know for a fact that the two rows will have exactly similar names up until the middle initial. In the past, someone used a tool that exported names from one database to my SQL database, which included middle initials. Since then, I have imported another list that does not include middle initials. I am looking for the ones that have middle initials from that subset.
This is a very large topic and effort depends on what you consider as "similar" and what the structure of the data is. For example are you going to want to match Doe, Johnathan as well?
Several algorithms exist but they can be extremely resource intensive when matching name alone if you have a large data set. That is why often using other attributes such as DOB, or Email, or Address to first narrow your possible matches then compare names typically works better.
When comparing you can use several algorithms such as Jaro-Winkler, Levenshtein Distance, ngrams. But you should also consider "confidence" of match by looking at the other information as suggested above.
Issue with matching addresses is you have the same fuzy logic problems. 1st vs first. So if going this route I would actually turn into GPS coordinates using another service then accepting records within X amount of distance.
And the age old issue with this is Matching a husband and wife. I personally know a married couple both named Michael Hatfield. So you could try to bring in gender of name but then Terry, Tracy, etc can be either....
Bottom line is only go the route of similarity of names if you have to and if you do look into other solutions like services by Melissa data, sql server data quality services as a tool.....
Update per comment about middle initial. If you always know the name will be the same except middle initial then this task can be fairly simple and not need any complicated algorithm. You could match based on one string + '%' being LIKE the other then testing to make sure length is only 2 different and that there is 1 more spaces in it than the smaller string. Or you could make an attempt at cleansing/removing the middle initial, this can be a little complicated if name has a space in it Doe, Ann Marie. But you could do it by testing if 2nd to last character is a space.

How can I replace a field with a similar result in MySQL

Unfortunately, I have to deal with a lot of user submitted data, text fields rather than option boxes. I have imported it into my MySQL database as strings. I do all this to be able to run statistics quickly on the data like top 10 most common companies. The problem I have run into is that some of the rows have slightly different names for the same companies. For example:
Brasfield & Gorrie, LLC VS Brasfield and Gorrie
Britt Peters and Associates VS Britt, Peters & Associates Inc.
Is there some fairly straightforward MySQL command or external tool that will allow me to go through and combine these sort of rows. I know how to use REPLACE(), but I don't think it has the power to do this simply. Correct me if I'm wrong!
Taking this example:
Brasfield & Gorrie, LLC VS Brasfield and Gorrie
Assuming that I want to keep the first one, I would find all records that have the ID of the second one and update them to use the first, assuming that this table that has these titles also has an ID field for each one.
You would create a page in PHP that will allow you to administer this with mouse clicks, but it will require regular pruning since you allow users to enter this data. For future entries, you can try to apply the Levenshtein Distance and try to provide a suggestion based on available similar matches so that you can help guide the users to something that already exists rather than a new db entry.

Mysql - How to create column that automatically sums rows?

I have a mysql database table called "character" with columns like name, strength, intelligence, skill. I want to create a column that sums up strength, intelligence and skill AUTOMATICALLY. Is this possible?
I have seen loads of pages showing how to do queries. I did a query just fine, such as
select (str+intel+skl) as sum from character;
It returns a sum just fine.
But what I'm missing (don't understand) is how to:
either AUTOMATE that query into my mysql db (for example, when I do "select * from character", it will show "strength, intelligence, skill, sum"),
OR where/how to incorporate the mysql query into my rails app so that SUM shows up in real time, and when edits to, for e.g. strength occur, the SUM is updated accordingly.
A key point, I am summing columns across a row (strength, intelligence, skill), not summing a column (strengths of several characters).
Your best option is to use a trigger (or the similar code at the app level).
Using a view works too, but if you need it frequently you'll want it pre-calculated for performance reasons.
For an introduction to triggers, see:
http://dev.mysql.com/doc/refman/5.6/en/triggers.html
You probably want something like this:
CREATE TRIGGER character_sum_ins BEFORE INSERT ON character
FOR EACH ROW SET NEW.sum = NEW.strength + NEW.intelligence + NEW.skill;
CREATE TRIGGER character_sum_upd BEFORE UPDATE ON character
FOR EACH ROW SET NEW.sum = NEW.strength + NEW.intelligence + NEW.skill;
Alternatively, pre-calculate the sum in your app and store it accordingly.
Side note: ask yourself if you really need this stored though. As point out by another commenter, there might be no need for the auto-calculated data at all.
This should work, if I'm understanding your question correctly and the strength, intelligence, skill columns are numeric data types.
select strength, intelligence, skill, strength+intelligence+skill as sum
from character
As suggested, a view could then be created pretty easily with:
create view totals as
select strength, intelligence, skill, strength+intelligence+skill as sum
from character
You would want to make this into a stored procedure. The procedure could do the calculations, and you could read the results as though they were columns.
You could also probably get away with a simple view for this use case.
Don't do this in mysql, just add the values up when displaying them, seriously.
Or if you really want, sum them in mysql in your SELECT statement when returning them.
Don't use a trigger just to maintain a total of some numbers - they are a seriously screwy way of confusing all future develoeprs on the project.

Filter a MySQL Result in Delphi

I'm having an issue with a certain requirement to one of my Homework Assignments. I am required to take a list of students and print out all of the students with credit hours of 12 or more. The Credit hours are stored in a separate table, and referenced through a third table
basically, a students table, a classes table with hours, and an enrolled table matching student id to Course id
I used a SUM aggregate grouped by First name from the tables and that all works great, but I don't quite understand how to filter out the people with less than 12 hours, since the SQL doesn't know how many hours each person is taking until it's done with the query.
my string looks like this
'SELECT Students.Fname, SUM(Classes.Crhrs) AS Credits
FROM Students, Classes, Enrolled
WHERE Students.ID = Enrolled.StudentID AND Classes.ID = Enrolled.CourseID
GROUP BY Students.Fname;'
It works fine and shows the grid in the Delphi Project, but I don't know where to go from here to filter the results, since each query run deletes the previous.
Since it's a homework exercise, I'm going to give a very short answer: look up the documentation for HAVING.
Beside getting the desired result directly from SQL as Martijn suggested, Delphi datasets have ways to filter data on the "client side" also. Check the Filter property and the OnFilter record.
Anyway, remember it is usually better to apply the best "filter" on the database side using the proper SQL, and then use client side "filters" only to allow for different views on an already obtained data set, without re-querying the same data, thus saving some database resources and bandwidth (as long as the data on the server didn't change meanwhile...)