I know it is kind of foolish question but is it possible to change only integer part of decimal value.e.g suppose I have number 12.34 then I want to change only 12 and .34 remain as it is.
I have two tables. table X and table Y
Table X
id(int)| value(decimal)
-------|--------
1 | 12.43
2 | 3.54
3 |102.07
Table Y
id(int)| value(int)
-------|--------
1 | 32
2 | 76
3 | 8
I want to resultant table x like below
Table X
id | value
------|--------
1 | 32.43
2 | 76.54
3 | 8.07
Replace int part of value of table X with value of table Y.
Is it possible in mysql?(with out any function call, something like string replace).
To isolate the decimal portion of the values in the X table you may use the following expression:
X.value - FLOOR(X.value)
In other words, we can subtract off the whole number component.
SELECT
x.id,
y.value + x.value - FLOOR(x.value) AS value
FROM X x
INNER JOIN Y y
ON x.id = y.id;
Demo
Related
In this question...
How to represent X and Y axis data in an SQL table
...the poster asks how to create a MySql table that represents an Excel spreadsheet with x vs y type information.
I'm wondering if there is any SELECT that would give a result that looks like the original Excel format of column x vs column y? In other words, how to start with the table given in the answer and get back to the spreadsheet shown in the question.
Modified problem 1: Suppose the source MySql table does not have a unique mapping of x to y, and instead of a result with the value for x vs y, you want a table that shows the COUNT('value') for each x vs y combination. (i.e. a histogram for x vs y samples)
Modified problem 2: Same as modified problem 1, except that there are some combinations of x and y that don't exist in the MySql table, so you want the count to show as 0 in the SELECT result. (i.e. a histogram for x vs y samples, where not every combination of x vs y is in the table)
Here is an example of what I would like to achieve for "Modified problem 2":
x y value y 635 762 889
---------------- x ------------
762 635 1 762 3 4 1
762 635 100 -> 1016 1 0 1
762 635 7 1270 1 1 2
762 762 9
762 762 9
762 762 9
762 762 9
762 889 2
1016 635 1
1016 889 3
1270 635 2
1270 762 3
1270 889 3
1270 889 4
I am able to create a select with GROUP BY that will give me:
x y COUNT(value)
-------------------
762 635 3
762 762 4
762 889 1
1016 635 1
1016 889 1
1270 635 1
1270 762 1
1270 889 2
But it would more convenient for me if a single query could make the 2-D style table. I'd also like to account for the missing combination x=1016, y=762. I've gotten sort of close with GROUP_CONCAT(), but I end up with:
x | hist
----------
762 | 3,4,1
1016 | 1,1
1270 | 1,1,2
It would be nice to have the y values as column headers, but that isn't the end of the world. But the missing x,y combination is a non-starter. My real data is a much bigger histogram, and not knowing where the NULLs are is no good.
My current MySql skills are not up to the task. Or, maybe, it just cannot be done and I should stop trying. :D Hoping someone knows the answer. Thanks in advance!
Use my stored procedure from
MySQL pivot tables - rows to colums . Query. Prepare the data for pivotting in a view:
CREATE VIEW source_for_pivot
AS
SELECT x, y, COUNT(value) cnt
FROM test
GROUP BY x, y;
then simply
CALL pivot ('source_for_pivot', 'x', 'y', 'cnt');
fiddle
If complete list of y values is static, never-changed and well known, then you may find/use more simple solutions.
I want to select data by creating range from three given dates in ascending order and comma separated unique cities that lie between those dates. My table is:
id city dob
------ ------ ------------
1 x 1993-05-16
2 y 1994-07-14
3 x 1995-10-18
4 z 2008-01-05
5 k 2000-01-05
6 y 1991-01-05
7 y 1992-04-07
And i want output like this:
count(city) city range
------------ ------ ---------------------
3 y,x 1991-01-05:1993-05-16
3 y,x,k 1994-07-14:2000-01-05
1 z 2008-01-05
I have a table named contacts
id name value
1 a x
2 b c
3 c x
4 d x
5 e x
How I want to delete the rows that contain value of x ?
A simple SQL query will do.
DELETE * FROM contacts WHERE value='x'
Consider a function F[x;y] that generates a table. I also have two lists; xList:[x1;x2;x3] and yList:[y1;y2;y3]. What is the best way to do a simple comma join of F[x1;y1],F[x1;y2],F[x1;y3],F[x2;y1],..., thereby producing one large table?
You have asked for the cross product of your argument lists, so the correct answer is
raze F ./: xList cross yList
Depending on what you are doing, you might want to look into having your function operate on the entire list of x and the entire list of y and return a table, rather than on each pair and then return a list of tables which has to get razed. The performance impact can be considerable, for example see below
q)g:{x?y} //your core operation
q)//this takes each pair of x,y, performs an operation and returns a table for each
q)//which must then be flattened with raze
q)fm:{flip `x`y`res!(x;y; enlist g[x;y])}
q)//this takes all x, y at once and returns one table
q)f:{flip `x`y`res!(x;y;g'[x;y])}
q)//let's set a seed to compare answers
q)\S 1
q)\ts do[10000;rm:raze fm'[x;y]]
76 2400j
q)\S 1
q)\ts do[10000;r:f[x;y]]
22 2176j
q)rm~r
1b
Setup our example
q)f:{([] total:enlist x+y; x:enlist x; y:enlist y)}
q)x:1 2 3
q)y:4 5 6
Demonstrate F[x1;y1]
q)f[1;4]
total x y
---------
5 1 4
q)f[2;5]
total x y
---------
7 2 5
Use the multi-valent apply operator together with each' to apply to each pair of arguments.
q)raze .'[f;flip (x;y)]
total x y
---------
5 1 4
7 2 5
9 3 6
Another way to achieve it using each-both :
x: 1 2 3
y: 4 5 6
f:{x+y}
f2:{ a:flip x cross y ; f'[a 0;a 1] }
f2[x;y]
5j, 6j, 7j, 6j, 7j, 8j, 7j, 8j, 9j
In SQL Server Reporting Services 2008 R2, I have the following dataset.
Date | Value 1 | Value 2
--------------------------
Week 1 | 52 | 57
Week 2 | 49 | 63
Week 3 | 88 | 71
I have a Stacked Column Chart with the X axis of Date, and the Y axis of Value. The columns in the chart currenly shows Value 2 on top of Value 1. This shows the column as the total of Value 1 and Value 2. So for Week 1, it is 109 etc.
Now my question is, how would I get the chart to show the total of each column to be the highest Value in the dataset? This would still show both values but would have the entire column of the lowest value with the remaining value on top. So for week 1, the total would be 57. The column for Value 1 would be 52 and the column for Value 2 would be 5.
This may be confusing so I have added a dummy image of what I intend the final graph to look like.
Maybe you can write a query like this:
SELECT LEAST(value1, value2) as value1, GREATEST(value1, value2) as value2
value1 < value2 as color
FROM ...