Sort results by number of NOT NULL values

Salman A

I have a query where each row consists of 3 columns:

  1. Name
  2. Distance
  3. Proximity

I want to sort the rows based on number of NOT NULL (i.e. present) values exactly as follows:

  1. All values are present
  2. Two values are present in this order
    • Name and Distance
    • Name and Proximity
    • Distance and Proximity
  3. One value is present
    • Name
    • Distance
    • Proximity

Here is sample data (insert statements are sorted in the order i expect):

/*
CREATE TABLE #TEMP (
    Type      VARCHAR(100),
    Name      VARCHAR(100),
    Distance  VARCHAR(100),
    Proximity VARCHAR(100)
);
*/
INSERT INTO #TEMP VALUES ('AIRPORT', 'KBLI', '21mi', 'City')
INSERT INTO #TEMP VALUES ('AIRPORT', 'KBLI', '21mi',  NULL )
INSERT INTO #TEMP VALUES ('AIRPORT', 'KBLI',  NULL , 'City')
INSERT INTO #TEMP VALUES ('AIRPORT',  NULL , '21mi', 'City')
INSERT INTO #TEMP VALUES ('AIRPORT', 'KBLI',  NULL ,  NULL )
INSERT INTO #TEMP VALUES ('AIRPORT',  NULL , '21mi',  NULL )
INSERT INTO #TEMP VALUES ('AIRPORT',  NULL ,  NULL , 'City')

I have had some success with COALESCE statement but I am looking for something efficient and readable. Later I will change to four columns.

Bohemian

Assign a present value as if it were a number (a name=4, a distance=3, a proximity=2), then sum them and sort by that:

select ...
from ...
order by
  case when name is null then 0 else 4 end +
  case when distance is null then 0 else 3 end +
  case when proximity is null then 0 else 2 end desc

The trick here is that 3+2 > 4, so a distance and proximity beats a name.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sort Guava Multimap by number of values

Sort results out by number ends or scores codeigniter

Sort query results by the most number without repetition

Pandas Number of Unique Values and sort by the number of unique

Mysql - how to sort results by values in columns?

Pandas sort_values gives unexpected results

dataframe Sort_values giving improper results

How to sort 2 separate arrays by Number values?

Sort array values base from the number of segments

How to sort only number values in array?

MongoDB, sort the results by the number of matching elements between two arrays

Mongo query to sort results with maximum number of distinct attributes or keys?

I need to sort MYSQL results by a number passed in OR another column

How to sort query results by date then specific column values in mysql

Python dataframe sort values select n first results

Certain values give incorrect results for prime number checking function

Sort & Ranking Functions in Excel for Time AND Number Values in one Column

How to Sort JSON Decode by Number Values in Foreach PHP

Sort a list of integers on absolute values with higher precedence positive number

Values sorted as String but I want to sort them as number in Python

Count the number of missing values in each column and sort them

Get values from JSON file and sort by key number value

How can I group by the count number of column values and sort it?

How can I sort an array by number of occurrence of its values?

Pandas: Sort number of words in one column by the values of another

Produce output from JSON for values and sort them by number

How can I access string values in Results View where the number of results may vary?

sort with special sort number

Calling Pandas df.sort_values() multiple times on the same column gives different results?

How do I sort the results of this Python program from lowest to highest values?

TOP Ranking

HotTag

Archive