1

I have searched Google, Stackoverflow and numerous other website for 2 days now. I am not able to come up with the logic for it.

I have 5 columns.

col1 |col2  |col3  |col4  |  col5
2000 | null | 1000 | null | null
5000 | 1000 | null | null | null
null | null | null | null | null
1000 | 100  | 250  | 600  | 111
4000 | 400  | 350  | null | 111

Sorry for the messed up table above. Still new at stackoverflow.

Now, I want to write a select query which will do the following:
1) Arrange these 5 rows in such a way that the rows with the maximum not null values will appear first. And, gradually it ends with a row of maximum null values.
2) Not return a row where all the columns are null.

For the above example, we should get row4 first then row5 then row1 then row2. Note that row3 is not returned as all the values are null.

Till now I have tried the query below which has come close to solving it but its not exact enough.

SELECT * 
FROM   table 
WHERE  col1 IS NOT NULL 
        OR col2 IS NOT NULL 
        OR col3 IS NOT NULL 
        OR col4 IS NOT NULL 
        OR col5 IS NOT NULL 
ORDER  BY CASE 
            WHEN col1 IS NULL THEN 1 
            ELSE 0 
          END, 
          col1 DESC, 
          CASE 
            WHEN col2 IS NULL THEN 1 
            ELSE 0 
          END, 
          col2 DESC, 
          CASE 
            WHEN col3 IS NULL THEN 1 
            ELSE 0 
          END, 
          col3 DESC, 
          CASE 
            WHEN col4 IS NULL THEN 1 
            ELSE 0 
          END, 
          col4 DESC, 
          CASE 
            WHEN col5 IS NULL THEN 1 
            ELSE 0 
          END, 
          col5 DESC 
1

3 Answers 3

2

The simplest way is to count the number of null values:

select t.*
from t
where (col1 is not null) or (col2 is not null) or (col3 is not null) or
      (col4 is not null) or (col5 is not null)
order by (col1 is not null) + (col2 is not null) + (col3 is not null) +
         (col4 is not null) + (col5 is not null);

This uses the MySQL short-cut that treats a boolean value as an integer.

Sign up to request clarification or add additional context in comments.

Comments

1
SELECT col1, 
       col2, 
       col3, 
       col4, 
       col5, 
       Sum(col1 + col2 + col3 + col4 + col5) AS total 
WHERE  col1 IS NOT NULL 
       AND col2 IS NOT NULL 
       AND col3 IS NOT NULL 
       AND col4 IS NOT NULL 
       AND col5 IS NOT NULL 
ORDER  BY total 

1 Comment

All the above suggested solutions didn't work. They all return the correct data but its not ordered in such a way that the rows with maximum not null values come at the top. how do we sort it in such a manner.
1

You can count the number of NULL valued fields using IF:

SELECT *
FROM (
   SELECT *,
          IF(col1 IS NULL, 1, 0) +  IF(col2 IS NULL, 1, 0) +
          IF(col3 IS NULL, 1, 0) +  IF(col4 IS NULL, 1, 0) +
          IF(col5 IS NULL, 1, 0) AS count_nulls
   FROM table) AS t
WHERE t.count_nulls < 5 
ORDER BY t.count_nulls

Demo here

8 Comments

Still the sorting doesn't work. Should I do the sorting in PHP code or there can be a solution for this?
@prash32 It should work. Can you provide some sample data demonstrating the problem?
for the same columns as in the question your query returns with all the rows as it is and count_nulls = 0
@prash32 The issue that you mention is not reproducible. Please check the attached fiddle demo.
Hi, Yes works on my database as well but I think MySQL treats null values as empty string i.e. no value inside "" quotation marks. I have replaced col1 IS NULL with col1 == "" and it worked. Not sure why IS NULL doesn't work.
|

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.