So I have my columns listed as 10, 20, 30, 40, 50 and would like mySQL to select and round up numbers between two selected ranges. So for queries BETWEEN 11 AND 32 I would like it to include the data between 10 and 40.
As is, simply using BETWEEN 11 AND 32 will only get me the values listed between 20-30. Rounding to the nearest 10 also does not alleviate this problem.
try
BETWEEN (FLOOR(lower/10)*10) AND (CEIL(upper/10)*10)
with lower and upper to be integers like 11 and 32, which will result in
(FLOOR(11/10)*10) => (FLOOR(1.1)*10) => 1*10 => 10
(CEIL(32/10)*10) => (CEIL(3.2)*10) => 4*10 => 40
x BETWEEN y AND z is the functional equivalent of (y <= x) AND (x <= z). if you want records "outside" of the specified range, then you have to widen your range, or modify the out-of-range values to be IN range.
Related
I am trying to solve a problem with SQL.
I have a SQL database which has a table named "data", in this table you have a row named "points". There is like 10000 float values in this row.
I want to make a desktop application that can compare 10 consecutive values ( which i manually enter ) to his nearest 10 consecutive values in the database.
exemple :
i want to compare this list of 10 values ( that i enter ):
10.1 , 25.4, 2, 35, 45, 78.9, 41.1, 44, 1, 65
to the best list of 10 values in my database where the 10 values are the nearest to my 10 entered values ( IMPORTANT : VALUES HAVE TO BE CONSECUTIVE ).
You can see below what i want to do, i want to get the list of the 10 consecutive values that is the nearest to the 10 values i want to compare.
points ( 10000 values... )
points row : 10, 15.5, 14.3, 2, 1, 10.2, 55, 65.3, 41, 10, 25.2, 3, 34, 44, 78.8, 41.2, 41, 2, 66, 44, 25.1, 33.2, 45, 75, 98, 12, 11.2 etc etc
The 10 values in bold are the best nearest consecutive values:
10 is near to 10.1
25.2 is near to 25.4
3 is near to 2
34 is near to 35
44 is near to 45
78.8 is near to 78.9
41.2 is near to 41.1
41 is near to 44
2 is near to 1
66 is near to 65
Is there any way to do this with SQL Command ?
Thanks in advance.
One option uses a row-limiting correlated subquery:
select v.val,
(select val from mytable t order by abs(t.val - v.val) limit 1) as match_val
from (
select 10.1 as val
union all select 25.4
union all ...
) v
Basically the subquery executes for each row in the from clause (which is where you put the list of values): it scans the table, orders rows by the difference with the original value, and brings the value of top row only.
SQL tables represent unordered sets. You need a column to specify the ordering.
You can use lag() or lead() to bring 10 values together. Then you need a definition of closest. One possibility is to take the absolute value of the differences and add them up:
select t.*
from (select t.*,
lead(val, 1) over (order by <ordercol>) as val_2,
lead(val, 2) over (order by <ordercol>) as val_3,
. . .
lead(val, 9) over (order by <ordercol>) as val_10
from t
) t
order by abs(val - $val_1) + abs(val_2 - $val_2) + . . .
limit 1;
The $val_1, $val_2, and so on represent the values that you are passing in.
The rest is just sorting and taking a limit.
I have a column like below.
Value
_____
48
48
39
96
50
I want to divide this with 48.
From the above 5 values, row 1,2,4 can be divided, but 3,5 not. I have to do this with SQL.
EG:
select count(*) from tbl where value/42
Result: 2
You are asking for the remainder when an integer is divided by 48. That has a specific name in mathematics, the modulo operation. This is actually a very interesting (to some people) part of number theory and group theory.
Most databases support the modulo operator or function via %. So the idea is:
select value
from t
where value % 48 = 0;
This might be:
where value mod 48 = 0
where mod(value, 48) = 0
depending on the database.
Another method is:
where floor(val / 48) * 48 = val
You can do that with a simple where clause
SELECT value FROM table WHERE value%48 = 0
The modulus operator returns the remainder for the division of the operands which in fact means we are checking if the numbers are divisible (remainder is zero when numbers are divisible)
So, if you are trying to count what’s not divisible then simple use below query
SELECT count(value) FROM table WHERE value%48 != 0
You can use modulus(%) to find the result. Check below query it will help you.
declare #tbl table (val int)
insert #tbl values(48),(48),(39),(96),(50)
select * from #tbl where val % 48 =0
Output
val
48
48
96
I have columns in my sql table. I am wondering how can I add zero after numeric values in my columns. So for example:
I have values e.g 9, 2, 7, 10. I want to add a zero after these numbers. I want them to be 90, 20, 70, 100.
There are some values in the columns that already have 0s after them e.g 70, 20, 100. These ones should retain their values.
How do I go about this?
Just multiple the column by ten (not sure what your table name is, edit that query):
UPDATE __TABLE__
SET mt_ca1 = (mt_ca1 * 10),
mt_ca2 = (mt_ca2 * 10)
WHERE mt_ca1 < 100 OR mt_ca2 < 100;
It is not clear if you want to add zeroes after only double-digit numbers or all numbers. MySQL has substring functionality though you'd need to clarify your needs.
You can use CONCAT and CASE
assuming the column type is INT
use CASE to check if the value multiplied to 10 is less than or equal to 100.
SELECT
(CASE
WHEN (mt_ca1*10) <= 100
THEN CONVERT(CONCAT(mt_ca1,'0'),UNSIGNED INTEGER)
ELSE mt_ca1
END) AS mt_ca1
FROM yourtable
I am looking for a ROUND() type function that would allow me to round numbers to 1 decimal place but also to the nearest 0.5.
To illustrate:
19.425 => 19.5
19.124 => 19.0
Similarly:
12.654 => 12.5
12.845 => 13.0
As vissi said, to get the result you want you'll need 2 round statements. (To get to 1 decimal place)
SELECT ROUND(ROUND(19.425 * 2) / 2, 1) #19.5
SELECT ROUND(ROUND(19.124 * 2) / 2, 1) #19.0
SELECT ROUND(ROUND(12.654 * 2) / 2, 1) #12.5
SELECT ROUND(ROUND(12.845 * 2) / 2, 1) #13.0
You can multiply your number by two, round and then divide by two. Note, that the result may still be not very accurate (sth like 19.5000000000001).
So I have a column with different numbers and wish to categorize them by range within 30 minute intervals. So 5 would be 0-30, 697 would be 690-720, and 169 would be 150-180. I was first thinking of doing a case statement, but it doesn't look like Access 2003 supports it. Is there perhaps some sort of algorithm that could determine the range? Preferably, this would be done within the query.
Thank you.
Take the integer portion of (number / 30) using the Int function and multiply it by 30 to get your lower bound, then add 30 to that number to get your upper bound.
Examples
Int(5 / 30) = 0 * 30 = 0
Int(697 / 30) = 23 * 30 = 690
Use / (integer division) and * (multiplication).
5/30*30 = 0
697/30*30 = 690
169/30*30 = 150
...
Let x be your column with the values you want to catalogue, the in pseudo-SQL you have:
select ((x/30)*30) as minrange,
(((x/30)+1)*30) as maxrange
from yourtable
(you should take the integer part of the division).
Hope this helps.
This is fairly straight forward. You can just use the following.
(number \ 30) * 30
This will give you the lower index of your range. It does have one problem, which is that 30, 720, 180 etc, will be returned as themselves. This means your ranges either need to be 0-29, 690-719, etc, or have your caller take this into account.
This assumes you are using VBA where the '\' operator returns only the quotient. See more on VB operators here