I want to sort my table but it have NaN value in it,
the problem is in ruby/rails NaN is considered as highest and I want to make it as lowest instead?
I'm thinking about removing the NaN from the list ordering the non NaN list and adding the NaN's after that
anyone have more convenience approach?
my databse is postgresql and the column type is Decimal/BigDecimal and the values is Decimal/BigDecimal
the schema is
create_table "download_speeds", force: :cascade do |t|
t.bigint "location_id", null: false
t.string "operator_name"
t.decimal "avg"
t.decimal "min"
t.decimal "max"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["location_id"], name: "index_download_speeds_on_location_id"
end
and I want to sort/give rank to the downloads_speeds
based on avg
column descendingly
highest avg
is on top (rank 1)
apparently it is just the way postgresql ordering with NaN
the workaround for this is using SQL
syntax to change the NaN
into null
then set an options to set the null
value to be the last when ordering
DownloadSpeed.order(Arel.sql("nullif(avg, 'NaN') desc nulls last"))
related questions :
Postgres order column with NaN values
Why do NULL values come first when ordering DESC in a PostgreSQL query?