I have two existing columns and have created a new blank column.
Column1 Column2 NewColumn
A B A/B
C D C/D
When I try the following my NewColumn is populated with 1's and 0's. I would like a decimal representation of the proportion.
update MyTable
set NewColumnd = ((Column1/Column2)*1.00)
where Column2 != 0
You're performing the conversion too late - the division has already been performed using integer math, and than the conversion to float occurs. Maybe try:
update MyTable
set NewColumnd = ((Column1*1.00)/Column2)
where Column2 != 0
Although it should be noted that, if the formula should always hold, a computed column would be better than something produced via an UPDATE.
Related
I have a table which has column storing values as comma separated values.
I need to extarct specific value like '2' and 10 in ever row of the column.I tried substr but cannot arrive into result.How can we achieve this in msql
My table looks like as attached as screenshot
You could just use find_in_set():
select
channel,
find_in_set('2', channel) > 0 has_2,
find_in_set('10', channel) > 0 has_10
from mytable
This adds two columns to the table with a boolean value that indicates whether values 2 and 10 are available in channel.
I like to use the result from another query to address the column name. For this I like to use CONCAT(). But somehow it don't work; when I run this line I get 0 rows back:
SELECT * FROM cover WHERE CONCAT('c','9') = 1;
When I don't make use of CONCAT() it work perfectly:
SELECT * FROM cover WHERE c9 = 1;
And also CONCAT() seems to work. With this I get a result:
SELECT CONCAT('c','9');
I tried all solution from this question:
MySQL select with CONCAT condition
like this one, but i always got 0rows back:
SELECT * FROM (
SELECT id, CONCAT('c', '9') as target
FROM cover) base
WHERE target = "1"
My MySQL Version is; 10.1.16-MariaDB
It is bad schema design to splay an array across a bunch of columns. And, as you are discovering, it is hard to use the columns. Build another table for the c values.
Or...
With lots of 0/1 "columns", consider SET or BIGINT UNSIGNED; either will hold up to 64 boolean flags in a tiny fraction of the space. And, with different code, BLOB could be used.
To extract bit 22 from a BIGINT, ((col >> 22) & 1) will give you 0 or 1.
Consider using a case when, since the number of options is known beforehand (you can only access columns that exist):
SELECT id
FROM cover
WHERE case ?
when 1 then c1
when 2 then c2
when 9 then c9
end = 1
... where the question mark would be the provided value, like 9 in your example.
I have amounts stored as a varchar in my table.
When attempting to sum them it always returns 0.00.
Below is an example using only one record from the db.
SELECT col1, SUM(CAST(col2 AS DECIMAL(20,2))) derived1
FROM table
WHERE col3 = 'FIT'
AND col1 = '6211195'
GROUP BY col1
This returns one row with a 0 value.
By removing the SUM and CAST from the query, I can see that it is pulling the value as it should, but I can not sum or cast it, adding either of those breaks it and returns 0 again.
I have also tried converting the field to a decimal type and it just zeros all the values.
EDIT:
Ughh, I just ran a REGEX query to detect anything that isnt an alphanumeric value or a decimal point. It appears that there are non ascii characters in the field that I cant see, messing with the type casting. Will continue to update as I learn more.
Show some sample values of col2. A string that really is a number is treated as a number. Hence '1' will become 1.
However, if the string does not start with a digit, '-', or '+' (after leading spaces), then it will be 0 (in most cases). So 'A1' will be 0. And so on. As will '$100'.
The lesson is: If a column contains numbers, store them as numbers. Really simple.
I have two tables x and y I want a column from x to be set to the average of a column from y grouped by a common column.
This what I'v done so far
update
set x.column2 = (SELECT AVG(NULLIF(column2,0))
FROM y group by column1)
on (x.column1 = y.column1)
And I want the value of x.column2 to be updated automatically whenever the value of any row of y.column2 changes.
Note: there is no column have the same name in the two tables.
UPDATE
x
SET
x.column2 = (SELECT AVG(NULLIF(column2,0))
FROM y
WHERE y.column1 = x.column1)
This will run the subquery once per row in x, but the subquery is limited to the rows in y where column1 matches the current x.column1.
For the curious, the internals of this are a bit deeper. In general, all queries (even sub-queries) return table-like objects ("relation" in relational-speak). If the result has only one row, it can be coerced into a 'row' ("tuple" in relational-speak). If the tuple has only one column, it can be further coerced into the value in that column. That is what is going on here. Additionally, no explicit "group by" is needed, because the WHERE clause limits the subquery to only the rows we want to sum, and so we take advantage of the implied 'group all rows' behavior (analogous to adding GROUP BY y.column1)
After your comment, I wanted to show how you would create a "View" for the same thing, which in MySQL means that the aggregated value is not actually 'stored', but calculated in real-time. This means it is never out of date as you insert into y.
CREATE VIEW vx AS SELECT column1, AVG(NULLIF(column2,0) as avg FROM y GROUP BY y.column1
You can then select from vx and in the background it will run that query.
You will need a trigger - see here.
In your case something like
CREATE TRIGGER name AFTER INSERT ON y FOR EACH ROW BEGIN [above statement] END
CREATE TRIGGER name AFTER UPDATE ON y FOR EACH ROW BEGIN [above statement] END
CREATE TRIGGER name AFTER DELETE ON y FOR EACH ROW BEGIN [above statement] END
I did not try out this, so no guarantee for being free of syntax errors (but should not be).
I have a COLUMN named Col1 in a table.
I want to divide each value in the column Col1by x if the value is less than K and by y if the value is more than K. So I try to update a value by itself and a factor.
I tried something like that but it didn't work :
UPDATE table1 SET Col1 = (SELECT Col1 FROM table1 AS) / IF(val < K, x, y));
I hope you will get me.
Thanks for your help.
(I should be reading a Mysql tutorial, my apologies but I'm not used to handle Mysql and I make query one time a year...)
UPDATE table1 SET Col1 = IF(col1 < K, col1/x, col1/y);