3

5, Not sure what am I doing wrong, please help. As it seems in a query on multiple column comparison to find least value, NULL continues to show up as a Resultant instead of least value

SELECT
  IF(col1 IS NULL OR col2 IS NULL OR col3 IS NULL OR col4 IS NULL OR col5 IS NULL, 
      COALESCE(col1,col2,col3,col4,col5), 
      LEAST(col1,col2,col3,col4,col5)
  ) As Resultant 
from db.tablename 
Group by Id;

Alternatively tried CASE select without much success. Thanks

5
  • 1
    What is the purpose of the GROUP BY? You have no aggregates in there. Commented Jan 31, 2012 at 17:27
  • Are you sure that at least 1 of col is not null ? Commented Jan 31, 2012 at 17:30
  • Sorry, it is Select *, Resultant; there are Id categories. Commented Jan 31, 2012 at 17:36
  • @a1ex07 Scenario is , there are not null/null values in all columns. If col1, col2 are null in a row, there are col3, col4, col5 with values/ not null in the same row. I tried assigning value to Null as '999' [ Select *, MIN(IFNULL(Col1, '999'), MIN(IFNULL(Col2, '999')...] but that didnt seem to work either. Commented Jan 31, 2012 at 17:40
  • Is Id the PRIMARY KEY of the table? Commented Jan 31, 2012 at 18:31

2 Answers 2

1

if null is considered 0

select least(ifnull(col1,0),ifnull(col2,0),ifnull(col3,0),ifnull(col4,0),ifnull(col5,0))
as Resultant
from db.tablename
Group by Id;

if null is condered max

select least(ifnull(col1,~0>>1),ifnull(col2,~0>>1),ifnull(col3,~0>>1),ifnull(col4,~0>>1),ifnull(col5,~0>>1))
as Resultant
from db.tablename
Group by Id;
Sign up to request clarification or add additional context in comments.

17 Comments

Thanks @ Melvin, ran both queries Resultant for a Col1 (null), Col2(null), Col3(not null), Col4(not null), Col5(not null) Brings Resultant (null) for that row, only when all Col1 to Col5 are not null the resultant fetches not null and that is also the least, correct, value amongst all columns.
are you saying that the ifnull(Col1,0) does not result to 0 when Col1 is null?
Instead of ifnull( col1,0) I changed to ifnull(col1,999) with '999'as the value for null, so that I can have non null/least value of the five columns as resultant values. But the resultant bring null instead of least value that exists in five columns when compared. It also brings least value of the five when there is no null value substituted.
ifnull(col1,0) is if we are going to treat nulls as 0 value, while ifnull(col1,~0>>1) is if we are going to treat nulls as max int value remember that you may reach the point where 999 is also a result of your query or could be higher making nulls interpreted as 999 to be the least.
Thanks again,Mevin, though I would appreciate your help in resolving this: there are not null/null values in all columns from Col1 to Col5. If col1, col2 are null in a row, there are col3 (160), col4 (170), col5(170) with values/ not null in the same row. I want to ignore Null Values and select the least from the non null values ( which would in this case be (160), Am I following the correct query to find the resultant value that is lowest /least when compared in Five columns with and without nulls? OR IF you know any alternative to GREATEST_NONNULL(...); or Least_NONNULL(col1, col2...) ?
|
0

For people with the case such that there are only two values to compare, you can do this:

GREATEST(ifnull(a,b), ifnull(b,a))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.