mysql database column issue - mysql

Create a list of all players and the fees they pay. The football league is looking to increase fees by 15%. Show that column in your list. When complete your output should look like the following. Note: your spacing may be a little different. This output is formatted to be smaller.
[1]: https://i.stack.imgur.com/RmN6D.png - Question with the format required
[enter image description here][1]
[2]: https://i.stack.imgur.com/iJNVS.png - Picture of the table that is going to be used. The table name is 'playerrec'
I know that you will have to use an alias for the column Fee it would be renamed to Increased Fee, but where I am getting stuck at is that I don't know what to do for the New Fee since it isn't a column that is existing. I've already thought about creating column, but I looked at my teachers lectures and it seems we won't be getting into that for a bit.

You can operate on a column in your SELECT clause using arithmetic operators.
If you were looking to get the base fees off the playerrec table, you could do so by simply selecting the column
SELECT pr.Fee
FROM playerrec AS pr
If you were looking to get, say, double the normal fee, you could do so like this:
SELECT (2 * pr.Fee) AS DoubledFee
FROM playerrec AS pr
Here you're computing a new column and aliasing it as 'DoubledFee'. This does not change the underlying data nor add a column to the table. If you want a comparison, you could select both together
SELECT Fee, (2 * pr.Fee) AS DoubledFee
...
For more, see https://learn.microsoft.com/en-us/sql/t-sql/language-elements/multiply-transact-sql?view=sql-server-ver15
As a bonus, the microsoft example includes a 15% change

Related

how to use multiple tables without duplication in tableau

I've trouble understanding how this should work...basically I've 2 main tables, in one I've Revenues, in another Costs.
Revenues table has fields as: P&L (string), Category (string), Products (string), Sold (int), invoiced (int), delivered (int), date (date).
Costs table has: P&L (string), Category (string), Products (string), Costs (int), date (date).
I'd like to use tables together to perform various calcs like margin, for example, at any level (total margin, which means total revenues - total costs, or at Category level for which I should be able to filter any category I have and perform the calc and so on).
Problem is, any tentative I've made to use relations or join, resulted in duplications.
The only workaround I was able to perform so far is to leave revenues table as it is, and create many Costs table, 1 for field basically (table1 with category and costs plus date, table2 with products, costs and date etc.). Joining Revenues with one of these tables seems to work but, in this way, I'm not able to create a wider view (one goal is to make a big table in the viz where we could read at once all the data). Plus, another problem I 've seen it appear doing this workaround is that, if I want to split by date costs, but I use the date column from the revenues table, even if the date is the same (I've done a copy/paste between tables basically), tableau doesn't recognize the date correctly, so to split costs, I've to use costs'date column, and to split revenues, I've to use revenues' date columns, which is frankly a pain...
So my question: how could I merge the 2 tables in one, or anyway how could I put all the data together in a working table to perform any kind of calcs,and also how could I use just 1 column for date that works for all the date altogether?
I've upload a file here to understand better what I'm trying to combine. Thank you guys
Data file
ps.: seems that tableau is using sql behind for these tasks so probably someone skilled in this kind of problem in sql could also help...for this I 've tagged sql as well, thanks
You need to UNION those 2 tables together, but are they really in Google or you just did that to demo it here?
If you're using Excel - both Revenue & Cost must be different sheets in the same XLS file
If you're using CSV - both Revenue & Cost must be different files (hopefully in the same folder)
I would really hope that you're using a database (some form of SQL), but either of the above options, UNION the data and it will work the way you expect :)

Making a custom table from one table

My table is called Storage_Tot which has date, tagid, value components.
Below are the two tables (Table A and Table B) I created using SQL query. I essentially ran same query with different tagid with the same table 'Storage_Tot). I would like to combine table A and B and also add a 3rd column which is the sum of the values of Table A and Table B (Table C).
Table A (Left table) Table B (Right Table)
enter image description here
Here is the sample query that I wrote for the two tables.
enter image description here
This is how I would like my final table to look like. As a first step I was trying to just combine the the values of Table A and Table B and after that focus on the arithmetic aspect.
enter image description here
Here is the SQL query when combing the two tables but it does not work
enter image description here
_______________________________________ APPEND
Since I couldn't post under the response question, After implementing your suggestion here is the error that I get.
enter image description here
This is how the query looks as it sits and it appears that its not like where Storage_Tot.TagID = 106
enter image description here
I believe what you are asking for is (1) a JOIN and (2) a calculated column.
Here's the general idea for a JOIN:
SELECT
tblA.date, tblA.value, tblB.value
FROM
(SELECT * FROM Storage_Tot WHERE tagid = 'TABLEA_TAGID') tblA
INNER JOIN
(SELECT * FROM Storage_Tot WHERE tagid = 'TABLEB_TAGID') tblB
ON (tblA.date = tblB.date);
Some of that is guesswork, because you didn't provide complete details in your question. But hopefully you can see what's going on: the sub-queries in a way 'create' what you are calling the two tables shown in your first image.
Now, to add a total, just add a calculated column to the above query:
SELECT
tblA.date, tblA.value, tblB.value, tblA.value + tblB.value AS total_value
I have not verified any of this in an instance of MySQL, but I do believe I have the syntax correct for MySQL. You might need to make small adjustments for typos.. I have verified the above syntax via SQL Fiddle. Of course you need to fill in what TABLEA_TAGID and TABLEB_TAGID really are.
There are also lots of tutorials and references for using JOINs on-line. There are various kinds of JOINs, and I'm only showing one kind here.
#landru27: Thank you so much for your help. I don't know how I missed that from/where syntax. Anyway, I took your direction and had to make minor changes and the code executed.
Below is the final code that I shall be using for my query.
enter image description here

Matching items in one table with their price in another table

So I have a Query with a list of "Items" Say 'APPLE', 'Carrot', and 'Pear', and a field beside it called "Quantity". On a another table I have a column with a field called "Item", then beside it a field with "cost". (Thus to show the item and its cost.) What I want to do is search the list for the item then take that item and times the price by the quantity, and show it on the chart.
Let's also assume that in the table of items that some of the spelling is not exact so one person may have typed apple and another apples, but in my item column it just says Apple, how do I make sure that they will find each other or the closest match on the list?
I have done a little work and I assume I have to create a join of some sort, but I am not sure of the expression to do the paring or the prices with the items once that is done a simple multiplication expression can be done.
You are right that you will need to perform a JOIN between the two tables/queries to match up the rows that correspond to each other ("apples to apples", so to speak). If your first table/query was called [Orders] and your second table/query was called [Prices] then a simple query to "do the math" would be something like
SELECT
Orders.Item,
Orders.Quantity,
Prices.Price,
Orders.Quantity * Prices.Price AS Cost
FROM
Orders
INNER JOIN
Prices
ON Orders.Item = Prices.Item
Regarding your point...
Lets also assume that in the table of items that some of the spelling
is not exact so one person may have typed apple and another apples,
but in my item column it just says Apple, how do I make sure that they
will find each other
...the short answer is: You design your database in such a way that such discrepancies cannot occur. Use your favorite Internet search engine to do some research on the following topics:
Data Validation
Referential Integrity
Edit re: comment
I was just wondering if I could use a Like function with % sign of some sort to search for the closest answer to solve the [matching] problem
Generally speaking, no. Those types of approaches can be very difficult to implement reliably (think "apples" vs. "pineapples") and can be very difficult to audit if incorrect matching occurs.
If you have dirty data then you're most likely just going to have to get busy and clean it up.

MySQL Database Design Questions

I am currently working on a web service that stores and displays money currency data.
I have two MySQL tables, CurrencyTable and CurrencyValueTable.
The CurrencyTable holds the names of the currencies as well as their description and so forth, like so:
CREATE TABLE CurrencyTable ( name VARCHAR(20), description TEXT, .... );
The CurrencyValueTable holds the values of the currencies during the day - a new value is inserted every 2 minutes when the market is open. The table looks like this:
CREATE TABLE CurrencyValueTable ( currency_name VARCHAR(20), value FLOAT, 'datetime' DATETIME, ....);
I have two questions regarding this design:
1) I have more than 200 currencies. Is it better to have a separate CurrencyValueTable for each currency or hold them all in one table?
2) I need to be able to show the current (latest) value of the currency. Is it better to just insert such a field to the CurrencyTable and update it every two minutes or is it better to use a statement like:
SELECT value FROM CurrencyValueTable ORDER BY 'datetime' DESC LIMIT 1
The second option seems slower.. I am leaning towards the first one (which is also easier to implement).
Any input would be greatly appreciated!!
p.s. - please ignore SQL syntax / other errors, I typed it off the top of my head..
Thanks!
To your questions:
I would use one table. Especially if you need to report on or compare data from multiple currencies, it will be incredibly improved by sticking to one table.
If you don't have a need to track the history of each currency's value, then go ahead and just update a single value -- but in that case, why even have a separate table? You can just add "latest value" as a field in the currency table and update it there. If you do need to track history, then you will need the two tables and the SQL you posted will work.
As an aside, instead of FLOAT I would use DECIMAL(10,2). After MySQL 5.0, this will actually have improved results when it comes to currency handling with rounding.
It is better to have one table holding all currencies
If there is need for historical prices, then the table needs to hold them. A reasonable compromise in many situations is to split the price table into a full list of historical prices and another table which only has the current prices.
Using data type float can be troublesome. Please be sure you know what you are doing. If not, use a database currency data type.
As your webservice is transactional it is better if you'd have to access less tables at the same time. Since you will be reading and writing a lot, I would suggest having a single table.
Its better to insert a field to the CurrencyTable and update it rather than hitting two tables for a single request.

I need some sort of full text search on mysql database

I've stuck with one quite tricky problem.
I have list of products from different warehouses, where each product have: Brand and Model plus some extra details. Model could be quite different from different warehouses for the same product, but Brand is always the same.
All list of products I store in one table, let's say it will be Product table.
Then I have another table - Model, with CORRECT Model Name, Brand and additional details like image, description etc. Plus I have keywords column where I try to add all keywords manually.
And here is the problem, I need to associate each product that I receive from warehouse with one record from my Model table. Right now I'm using full text search in boolean mode, but that's quite painful and does not work very well. I need to do a lot of manual work.
Here are just few examples of names that I have:
WINT.SPORT3D
WINT.SPORT3D XL
WINT.SPORT 3D
WINT.SPORT3D MO
WINTER SPORT 3D
The correct name for all of these items would be: WINTER SPORT 3D, so they should all be assigned to the same model.
So, is there any way to improve full text search or some other technique to solve my problem?
Database that I'm using is MySQL, I would prefer not to change it.
I'll start by putting together a more formal definition of the tables:
warehouse:
warehouse_id,
warehouse_product_id,
product_brand,
product_name,
local_id
Here I'd using local_id as a foreign key to your 'Model' table - but to avoid further confusion, I'll call it 'local'
local:
id,
product_brand,
product_name
It seems like the table you describe as 'product' is redundant.
Obviously until the data is cross referenced, local_id will be null. But after it is populated it won't have to change, and given a warehouse_id, a band and a product, you can find your local descriptor easily:
SELECT local.*
FROM local, warehouse
WHERE local.id=warehouse.local_id
AND warehouse.product_brand=local.product_brand
AND warehouse_id=_____
AND warehouse.product_brand=____
AND warehouse.product_name=____
So all you need to do is populate the links. Soundex is a rather crude tool - a better solution for this would be the Levenstein distance algorithm. There's a mysql implementation here
Given a set of rows in the warehouse table which need to be populated:
SELECT w.*
FROM warehouse w
WHERE w.local_id IS NULL;
...for each row identify the best match as (using the values from the previous query as w.*)....
SELECT local.id
FROM local
WHERE local.product_brand=w.product_brand
ORDER BY levenstein(local.product_name, w.product_name) ASC
LIMIT 0,1
But this will find the best match, even if the 2 strings are completely different! Hence....
SELECT local.id
FROM local
WHERE local.product_brand=w.product_brand
AND levenstein(local.product_name, w.product_name)<
(IF LENGTH(local.product_name)<LENGTH(w.product_name),
LENGTH(local.product_name), LENGTH(w.product_name))/2
ORDER BY levenstein(local.product_name, w.product_name) ASC
LIMIT 0,1
...requires at least half the string to match.
So this can be implemented in a single update statement:
UPDATE warehouse w
SET local_id=(
SELECT local.id
FROM local
WHERE local.product_brand=w.product_brand
AND levenstein(local.product_name, w.product_name)<
(IF LENGTH(local.product_name)<LENGTH(w.product_name),
LENGTH(local.product_name), LENGTH(w.product_name))/2
ORDER BY levenstein(local.product_name, w.product_name) ASC
LIMIT 0,1
)
WHERE local_id IS NULL;
Try Soundex. All of your examples resolve to W532 while the last one resolves to W536. So, you could:
Add a column to PRODUCT and MODEL called SoundexValue and calculate the Soundex value for each product and model
Compare the Soundex values in the PRODUCT table to the ones in the Model Table. You may have to use a range (+/- 5) to get a higher rate of matching.
Follow the 80/20 rule. That is, spend 80% of your manual effort on the 20% that don't easily fall out.