Is there a built-in function or some other way to limit the value of numbers in MySQL?
For example, let's say I have a table with the following rows:
score
=====
5
10
20
50
3
15
I'd looking for some type of query along the lines of...
SELECT NUMBER_LIMITER(score, 10) FROM ScoresTable
...which would return the following result...
score
=====
5
10
10
10
3
10
If not, I'll write a function. Just hoping there might be something built-in as performance is quite important for this project. Thanks!
SELECT LEAST(score, 10) FROM ScoresTable
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html#function_least
Related
I'm using the MYSQL NTILE function and for the most part it is doing what I need it to, however there is one case in which I need different behaviour and I can't figure out how to do it. The case is when I have more buckets than I do records.
So lets say my data in a table called data looks like this
ID val
1 15
2 20
3 10
My issue is when I have more buckets than I do records, so lets say I run
select *, NTILE(4) over (order by val) from data
This will result in
ID val NTILE
3 10 1
1 15 2
2 20 3
I'm having some trouble wording my question which is probably why I am struggling to find solutions on Google, but basically my question is this: Is there any way that when I have more buckets than records (in this example 4 buckets but only 3 records) that I can treat the highest value as the highest percentile and work backwards rather than what it is currently doing which is treating the lowest value as the lowest percentile? Essentially resulting in this:
ID val NTILE
2 20 4
1 15 3
3 10 2
I think you might be able to reverse the ordering in the NTILE() and numerically flip the result like so:
select *, 5-NTILE(4) over (order by val desc) from data
I would expect the following to happen (I have not run this though!):
ID val NITLE
2 20 4
1 15 3
3 10 2
I am new in MySql and help will be much appreciated
I had this assignment which is to get the sum and average of dynamic rows in the table. The table looks like
tbl_grade
id scores
1 10
1 11
1 9
1 10
1 6
2 10
2 9
2 10
I want to show the results like this
id sum average
1 46 9.2
2 29 9.7
Hope anyone can help. Thanks
This is a simple query using GROUP BY clause and the aggregate methods SUM and AVG. For a better understanding about grouping and aggregate methods, please read next: http://www.mysqltutorial.org/mysql-group-by.aspx
SELECT
id,
SUM(scores),
AVG(scores)
FROM
tbl_grade
GROUP BY
id
ok, i'm not sure if i can explain this right.
Lets say i have a table with three columns (id, price, maxcombo)
maybe there's like 5 rows in this table with random numbers for price. 2. id is just incremental unique key)
maxcombo specified if that price can be in a combination of up to whatever number it is.
If x was 3, i would need to find the combination that has the maximum value of the sum 1-3 columns.
So say the table had:
1 - 100 - 1
2 - 50 - 3
3 - 10 - 3
4 - 15 - 3
5 - 20 - 2
the correct answer with be just row id 1.
since 100 alone (and can only be alone based on the maxcombo number)
is greater than say 50 + 20 + 15 or 20 + 15 or 10 + 20 etc.
Does that make sense?
I mean i could just calculate all the diff combinations and see which has the largest value, but i would imagine that would take a very long time if the table was larger than 5 rows.
Was wondering any math genius or super dev out there had some advice or creative way to figure this out in a more efficient manner.
Thanks ahead of time!
I built this solution to achieve the desired query. However, it hasn't been tested in terms of efficiency.
Following the example of colums 1-3:
SELECT max(a+b+c) FROM sample_table WHERE a < 3;
EDIT:
Looking at:
The correct answer will be just row id 1
...I considered maybe I misunderstood your question, and you want the query just obtain the rowid. So, I made this other one:
SELECT a FROM sum_combo WHERE a+b+c=(
SELECT max(a+b+c) FROM sum_combo WHERE a > 3
);
Which would for sure take too long in larger tables than just 5 rows.
say if the table trap has column 'id' and rows contents as
7
8
9
10
11
12
13
14
SELECT id FROM trap
WHERE id<10 and id>12
this doesn't give any output
but if
SELECT id FROM trap
WHERE id>7 and id<14
give me the required output i.e.,
8
9
10
11
12
13
The problem is that you're using the AND operator where you should be using OR.
SELECT id FROM trap WHERE id<10 OR id>12
Obviously id cannot be both 10 AND 12 at the same time, one box can only hold one value.
Alternatively you can write the statement as:
SELECT id FROM trap WHERE NOT(id BETWEEN 10 AND 12)
The reason that SELECT id FROM trap WHERE id>7 and id<14 does work is that it is possible for a value to be BETWEEN 8 AND 13 (inclusive) at the same time.
However no way can a value ever the smaller than 10 and larger than 12 at the same time.
So if the conditions are mutually exclusive you must use OR, if the conditions do not exclude one and other then you must use AND.
You can wrap a test inside a NOT() to reverse the test, this is because AND and OR are exact opposites.
I think you're mistaken, or you've phrased the question wrong.
It's impossible for id < 10 and id > 12 to be true at the same time. That's why you aren't getting any results for your first query.
What you're looking for is:
SELECT id FROM trap WHERE id<10 OR id>12
I'm way out of my league here...
I have a mapping table (table1) to assign particular values (value) to a whole number (map_nu). My second table (table2), is a collection of averages (avg) for each user (user_id).
(I couldn't figure out how to properly make a markdown table, please feel free to edit!)
table1: table2:
(value)(Map_nu) (user_id)(avg)
---- -----
1 1 1 1.111
1.045 2 2 1.2
1.09 3 3 1.33333
1.135 4 4 1
1.18 5 5 1.389
1.225 6 6 1.42
1.27 7 7 1.07
1.315 8
1.36 9
1.405 10
The value Map_nu is a special number that each user gets assigned according to their average. I need to find a way to match the averages from table2 to the closest value in table1. I only need to match to the 2 digit past the decimal, so I've added the Truncated function
SELECT table2.user_id, map_nu
FROM `table1`
JOIN table2 ON TRUNCATE(table1.value,2)=TRUNCATE(table2.avg,2)
I still miss the values that don't match the averages exactly. Is there a way to pick the nearest truncated value or even to round to the second decimal? Rounding up/down wont matter as long as its applied to all values the same.
I am trying to have the following result (if rounded up):
(user_id)(Map_nu)
----
1 4
2 6
3 6
4 1
5 10
6 11
7 3
Thanks!
i think you might have to do this in 2 separate queries. there is no 'nearest' operator in sql, so you can either calculate it in your software, or you could use
select map_nu from table1 ORDER BY abs(value - $avg) LIMIT 1
inside a loop. however, that cannot be used as a join function as it requires the ORDER and LIMIT which are not valid as joins.
another way of looking at it is it seems that your map_nu and value are deterministic in relation to each other - value = 1 + ((map_nu - 1) * 0.045) - so maybe you could make use of that fact and calculate an integer based on that equation? assuming that relationship holds true for all values of map_nu.
This is an awkward database design. What is the data representing and what are you trying to solve? There might be a better way.
Maybe do something like...
SELECT a.user_id, b.map_nu, abs(a.avg - b.value)
FROM
table2 a
join table1 b
left join table1 c on abs(a.avg - b.value) > abs(a.avg - c.value)
where c.value is null
order by a.user_id
Doesn't actually produce the same output as the one you were expecting for (doesn't do any rounding). Though you should be able to tweak it from there. Above query will produce the output below (w/ data you've provided):
user_id map_nu abs(a.avg - b.value)
------- ------ --------------------
1 3 0.0209999999999999
2 5 0.02
3 8 0.01833
4 1 0
5 10 0.016
6 10 0.0149999999999999
7 3 0.02
Beware though if you're dealing with large tables. Evaluate the explain of the above query if it'll be practical to run it within MySQL or if better to be done outside it.
Note 2: Will produce duplicate rows if there are avg values that are equi-distant to value values within table1 (Ex. if value for map_nu's 11 and 12 are 2 and 3 and someone get's an avg of 2.5). Your question doesn't really specify what to do for that so you might want to take that into account.
Its taking a little extra work, but I figure the easiest way to get my results will be to map all values to the second decimal place in table1:
1 1
1.01 1
1.02 1
1.03 1
1.04 1
1.05 2
1.06 2
1.07 2
1.08 2
1.09 3
1.1 3
1.11 3
1.12 3
1.13 3
1.14 4
...
Thanks for the suggestions! Sorry I couldn't present the question more clear.