Specify how nulls are sorted in the attribute table (nulls first or last)

1760
6
06-21-2022 08:38 AM
Status: Open
Labels (1)
Bud
by
Notable Contributor

When sorting a field in the attribute table, it would be helpful if we could specify how nulls are handled (NULLS FIRST vs. NULLS LAST).

For example (Oracle), I want to sort a date field descending so that the latest dates are at the top. But when I do that, 1) nulls are at the top, then 2) the latest dates are shown next, then 3) the first dates. 

Bud_0-1655824079031.png

That's a nuisance when dealing with large tables. I need to painstakingly scroll down through nulls to get to the first instance of a non-null date in the list. (In my case, I had to scroll through 10,000 nulls...and then look for where the nulls stop and the dates start)

Sorting in ascending order doesn't help either.

It would be better if I could specify that I want the nulls to be last in the list, even though I'm sorting descending.

Maybe something like this:

Bud_2-1655824764508.png

Would something like that be possible?

 

Related: Allow using the ORDER BY clause in definition queries (SQL)

6 Comments
Bud
by

For what it’s worth:

Oracle, PostrgreSQL, and DB2 have this functionality built right into the database. 

with data (id, last_edited_date) as (
select 1, to_date('2022-06-21', 'YYYY-MM-DD') from dual union all
select 2, null                                from dual union all
select 3, to_date('2022-06-21', 'YYYY-MM-DD') from dual)

select
    id,
    last_edited_date
from
    data
order by
    last_edited_date desc nulls last  

        ID LAST_EDITED_DATE
---------- ----------------
         1        21-JUN-22
         3        21-JUN-22
         2           [null]   

 

Indexing ASC, DESC and NULLS FIRST/LAST

Besides ASC and DESC, the SQL standard defines two hardly known modifiers for the order by clause: NULLS FIRST and NULLS LAST. Explicit control over NULL sorting was “recently” introduced as an optional extension with SQL:2003.

Bud_0-1655825926613.png

So, since that functionality is built into some databases, that might make implementing this feature easy…at least for those specific DBs.

Edit:

It turns out there is a way to do it in all databases:

Null in Order By

The effect of  nulls (first|last) can be obtained with a case expression in all databases. The following example implements order by c nulls first:

ORDER BY CASE WHEN c IS NULL
              THEN 0
              ELSE 1
          END
       , c

Note that the case expression defines a new order by key for the sole purpose of separating null and not null values.


So I wonder if that would make it easy to implement this functionality in ArcGIS Pro. Just construct a slightly different ORDER BY clause in the SQL that's sent to the database...using the above CASE technique.

KoryKramer

You could select features where the date is not null, then switch to Show Selected Records and sort on the date field there:

KoryKramer_0-1655826973796.png

 

Bud
by

@KoryKramer 

Yeah, good point. That would work in a pinch.

It's still maybe a bit clunkier than I'd like though. And takes an extra step.

In many cases, I still want to see the nulls too, I just want them to be at the bottom of the list, while sorting descending.

Thanks for the tip though.

KoryKramer

Understood - just sharing some thoughts to give you something to work with.

Bud
by

I tested a different workaround, and it technically worked as intended. But it was far to slow to use:

1. Create a query layer with a NULL_FLAG field:

select
objectid,
last_edited_date,
case when last_edited_date is not null then 1 else 0 end as null_flag
--Source: https://modern-sql.com/concept/null#order-by
from
events

2. Join from the table to the query layer via the OBJECTID.

3. Do an advanced sort using the fields from the query layer:
 - NULL_FLAG descending.
 - LAST_EDITED_DATE descending.
 

Now, the latest dates are at the top and the nulls are at the bottom. Which is what I wanted.

But like I said, it's far too slow to use. It takes roughly a minute to do the sorting, whereas sorting was instantaneous without the workaround (and sorting was instantaneous when the query was tested in the DB too). So, something about joining and sorting on the query layer is going awry.

Esri Case #03092490 - Attribute table join (and sort) between two EGDB tables [performance issues]


Related: 

Bud
by