How to choose the n-th smallest, and an array with the condition - if in the adjacent column 1, then the value is taken into the array, if 0, then no
|
2 answers
nth least For MySQL, use the
LIMIT. For other DBMS, something similar
SELECT val FROM myTable GROUP BY val ORDER BY val LIMIT 1 OFFSET n; and an array with the condition - if the adjacent column is 1, then the value is taken into the array, if 0, then there is no
SELECT val FROM myTable WHERE test_col = 1; |
The row_number function (or its analogs) can help you. Example for sql-server:
WITH DT AS ( SELECT ROW_NUMBER() OVER(ORDER BY ColumnName) AS Num, * FROM TableName ) SELECT * FROM dt WHERE Num = 7 Or another option (works, starting with sql-server 2012):
SELECT * FROM TableName ORDER BY ColumnName OFFSET 6 ROWS FETCH NEXT 1 ROWS ONLY |
MIN()- MaximPro3or2in response? - Denis