MySQL query to match exact value or next lower value - mysql

A PHP script returns a value which must be looked up in the first column of a table in a MySQL database.
If the exact value returned by the script appears in that first table column, then the query needs to extract the further information from the other columns along that row.
If the script value doesn't appear in the first table column, then the same information should be extracted from the next lowest value, should it exist.
So the database has rows looking like this, with the target lookup value in the first column:
18 0.3783 0.4438 0.5155 0.5614 0.6787
19 0.3687 0.4329 0.5034 0.5487 0.6652
20 0.3598 0.4227 0.4921 0.5368 0.6524
25 0.3233 0.3809 0.4451 0.4869 0.5974
30 0.2960 0.3494 0.4093 0.4487 0.5541
If the returned script value is 25, then the five other other values in the same row:
0.3233 0.3809 0.4451 0.4869 0.5974
...should be extracted.
If the script value however is 24, then the row information with the next lowest value needs to be extracted, which would be that for the database first-column value 20, being:
0.3598 0.4227 0.4921 0.5368 0.6524
What sort of query would be best suited for this?

You can filter on rows whose first column is equal to or less than the parameter, order by descending value in that column, and keep the top row only, using limit.
Assuming that the filtering column is called id:
select *
from mytable
where id <= ?
order by id desc
limit 1
The question mark represents the parameter to the query.

Related

How to query Top N ARRAY_MAX in SQL?

My table has a column that is in array type, and each in each row contain an array of length of 100.
I want to get the top 10 value out of each array and I only know how to get top 1 value using ARRAY_MAX(column)
How can we get top N value instead?
Getting top N out of an array is different from getting top n value out of a regular column and rows where you can do
SELECT column \\ FROM table \\ ORDER BY column DESC \\ LIMIT 10
May I suggest you correcting the tag info if the question is not MySQL based ? But if we are indeed using MySQL as the MySQL tag implies or if you are simply curious about how MySQL copes with array, I believe it does not have an array type for columns and not really needs one. This can be accomplished using a primary table with an array_id column and a second table which references the primary table's array_id and stores individual values through each row. The second table may look like this:
array_id array_value
1 443
1 80
1 8088
2 3306
2 1521
3 22
Thus the TOP N values can be easily retrieved. If by TOP N values you mean the top N highest values , we can use a similar query as the one you provided: SELECT array_value from array_table where array_id=n ORDER BY array_value DESC LIMIT 10;
Or if you mean the top n values in terms of the sequence in the array, we can use:
SELECT array_value from array_table where array_id=n LIMIT 10;
In the latter case, no ORDER BY clause is required in this case as we would like to retrieve values based on the sequence of rows being inserted. Usually it's the default sequence for a column's values to show up without index if ORDER BY clause is omitted. However, if you are really worried about the sequence without ORDER BY, you are free to add a timestamp column or PK auto_increment id column and use it in the ORDER BY clause to guarantee the sequence.

SQL Window Functions- NTH_VALUE- Why does it return NULL if we use Order by for first n-1 rows

So, I am talking about the NTH_VALUE(column,n) in SQL
Now What I observed is that if I used to order by clause and specify the same column as inside nth_value, the first n-1 rows are always NULL in the selected column. Why is that? IF I want to select the 3rd column to say, and the 3rd column exists, it should return the 3rd column for row number 1 and 2 as well in spite of whether I use the order by(same column) clause or not?
ALso, any way to get around with it?
This happens because the default frame clause that sql uses is range between unbounded preceding and current row. This means it will only consider rows till the current row to get the nth value. So for the rows till N-1, you'll get NULL because Nth value is beyond these N-1 rows.
Solution:
If you change that to range between unbounded preceding and unbounded following inside over() then you won't get NULL values with the Nth value window function.

How to display only one maximum value for date in a MS query table using SQL (not a set of maximum values)?

I am attempting to display only one maximum value for a date value (I would like to see only one value in other columns for the newest possible date).
Are there any options how to achieve this in MS Query sql?
My table is this:
My current code is this:
SELECT stof_0."arti-cd-base", stof_0."dcmf-nr", Max(stof_0."stof-dt") AS 'Maximum z stof-dt', stof_0."stof-qty-op"
FROM NILOS.PUB.stof stof_0
GROUP BY stof_0."arti-cd-base", stof_0."dcmf-nr", stof_0."stof-qty-op", stof_0."arti-cd-sfx", stof_0."adfc-cd-diffco"
HAVING (stof_0."arti-cd-base"=1) AND (stof_0."arti-cd-sfx"=15) AND (stof_0."adfc-cd-diffco"=0)
ORDER BY stof_0."arti-cd-base", Max(stof_0."stof-dt") DESC
Would anyone know how to arrange the code so that only one value (one line) will be the result for the latest date in the table?
If you just one one row with the latest date in the table use order by and some form of limiting the results. In standard SQL, you can do:
select s.*
from NILOS.PUB.stof s
order by "stof-dt"
fetch first 1 row only;
In MySQL, you would use limit instead of fetch.
In SQL Server, you would use one of:
select top (1)
or:
offset 0 row fetch first 1 row only

mysql query conditional ordering precedence

I have a number of uniquely identified records/rows that are returned as results when selected by their shared identifier. I only want one record/result returned per group. If a given variable belonging to each record is set to a specific value, I want to return that row (which would always be the last instance, if it is set - there is always only one instance of the variable being set for a group of query results). If all the possible rows/results for an instance are reviewed and the variable for that group is not set (meaning the last row returned doesn't have that variable set), then I would want to return the FIRST row result (the first result sorted by a DIFFERENT variable).
so far I have
SELECT table1.var1, table1.var2, table1.var3, table1.var4 etc
from table1, table2
where table1.var1 = table2.var1
AND (condition that satisfies where the variable is set)
OR (condition where the variable isn't set but returns multiple results
that I need the first instance of, but ONLY IF the first condition
doesn't exist in the group of results)
GROUP BY table1.var1, table1.var2 etc
ORDER BY table1.var1, table1.var2 DESC,
table1.variablethatIthoughtwouldsortthesecondconditionbutdoesnt DESC;
I tried using ASC and DESC but the first condition will only be met in the last result, and I would only want the result in the OR condition if the first condition cannot be met - but it has to be the first instance of whatever results match the OR condition.
Any help is appreciated, and I am happy to further clarify if needed.
I'm basically looking for a way to give precedence to the first condition (select the record that whose variable is set if there is one) and select the first instance of the other conditional if it isn't.
In other words, I want any records that meet the first condition (there will only be one per group), and then I also want, from a different group, only the first result per grouping that meets the second condition but whose ID is not in the first group (because the record should only either be in the first group or the second group, not both).

Limit the result starting from a specific row with a given Id?

I want to write a query to select a subset of a table, only starting from a given id.
I know about limit x, y, but x here is the number of the raw to start from. But in my case I want to start from a specific Id, no matter what its location inside the table.
What I mean is that the query below selects from row number 5, but I want it to select 10 records from row with id, say 213odin2d211d21:
SELECT * FROM my_table Limit 5, 10
I can't find a way to do this. Any help will be appreciated.
Note that, the Id here is a mix of strings and integers. So I can't do
SELECT * FROM <table> WHERE id > (id)
What you want to do is not possible. By default, records in the database are not ordered. Without ORDER BY you can't expect the server to return your queries in any particular order. Since you are saying, that you store some kind of digit/char identifier as your id, for which less then and greater then are not defined, it is not clear which records "follow" your specific record.
You will either have to:
Define another column to sort your records on, or
Define a behaviour for comparing your ids (What is "less then"? What is "greater then"?)
That being said, you can of course define that you want to sort your id just like sorting strings! In this case, you can use STRCMP() to compare two strings. Your query would look like this:
SELECT * FROM <table> WHERE STRCMP(id,?) = 1 ORDER BY id LIMIT 10
This will select the first 10 records, with id "greater than" ?.