Below is my table and from column B I want to get the value 3 in results.
| A | B |
|---|---|
| 1 | 1 |
| 2 | Null |
| 3 | 2 |
| 4 | Null |
| 5 | 3 |
| 6 | Null |
| 7 | Null |
| 8 | Null |
Case not working: it should return 10
| A | B |
|---|---|
| 1 | 1 |
| 2 | Null |
| 3 | 2 |
| 4 | Null |
| 5 | 3 |
| 6 | Null |
| 7 | Null |
| 8 | 10 |
CodePudding user response:
Use a LIMIT query:
SELECT *
FROM yourTable
WHERE B IS NOT NULL
ORDER BY A DESC
LIMIT 1;
CodePudding user response:
Do you want to get the value '3' because it is the largest in 'B' column?
If so, you can run the following query:
SELECT *
FROM Table
WHERE B IS NOT NULL
ORDER BY B DESC
LIMIT 1;
CodePudding user response:
There is not much to go on in the question. However, assuming that you can order by the col A, the answer would be
SELECT * from table
where B is not NULL
Order by A DESC
Limit 1
If A is numeric, then the order by works fine and you get all 3 non null values from column B. Then Order by descending limit 1 will ensure you get last value of B
CodePudding user response:
You already received really good answers that should help you. I just want to add a futher propose that works fine if both A and B are numeric and they are always sorted like in your example, meaning the highest b entry always has the highest a value.
SELECT MAX(b) FROM tableB
WHERE b IS NOT NULL
HAVING MAX(a);
