ycv005

ycv005

How to sort by the boolean value?

I want to sort the people by the boolean value i.e., whose field will be true will come first then false one.
How I modified the following query

order_by: [desc_nulls_last: r.percent, asc: applicant.name, asc: applicant.boolean_field]

Your help will be appreciated. Is there any way or not?

First Post!

alexiss

alexiss

I think you should use fragments with respect to this stackoverflow answer:

order_by: [
  desc_nulls_last: r.percent,
  asc: applicant.name,
  asc: fragment("case when ? then 1 when ? is null then 2 else 3 end", applicant.boolean_field, applicant.boolean_field)
]

Most Liked

sribe

sribe

desc? Assuming you have no nulls, since you didn’t mention what to do with them–normal order is false then true, that’s PG’s idea of “ascending” for this data type, so to reverse it just use desc:

Where Next?

Popular in Questions Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44778 311
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement