cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

ORDER BY a hidden expression

I can use an expression to create a custom sequence number, and give it a name with AS, and then use that name in ORDER BY. In Neo4j browser, is it possible to suppress that column in the resulting list? It needs to defined in RETURN in order to sort by it in ORDER BY, but I don't really need to see it.

4 REPLIES 4

You can do what you're looking for by not including the sequence number in the RETURN clause. Guessing at your query for the example:


MATCH (p:Person)
WITH collect(p) AS peez, range(0,size(collect(p)) - 1) as nbrs
UNWIND nbrs AS nbr
RETURN peez[nbr] AS person
ORDER BY nbr DESC

╒═══════════════╕
│"person"       │
╞═══════════════╡
│{"name":"Anne"}│
├───────────────┤
│{"name":"Dan"} │
└───────────────┘

With sequence number


MATCH (p:Person)
WITH collect(p) AS peez, range(0,size(collect(p)) - 1) as nbrs
UNWIND nbrs AS nbr
RETURN nbr, peez[nbr] AS person
ORDER BY nbr DESC

╒═════╤═══════════════╕
│"nbr"│"person"       │
╞═════╪═══════════════╡
│1    │{"name":"Anne"}│
├─────┼───────────────┤
│0    │{"name":"Dan"} │
└─────┴───────────────┘

If I don't include the sequence number in the RETURN clause, it's not available t the ORDER BY clause.

Something like this:

To sort other than alphabetically:
with '<my data>' as site, '<my data value>' as param
match (o:Observation) where o.site = site and o.param = param
    return 
    case o.env
      when 'Latest' then 1
      when 'Stage' then 2
      when 'Load' then 3
    end as seq,
    o.env as env, max(o.dateTime) as dateTime, o.value as value
    order by seq

3X_9_7_979cc1ae28a8565213f887ac4aa39d62a685b2bd.png
I want to sort by, but not show, the "seq" column.

Try this:

with 'my data' as site, 'my data value' as param
match (o:Observation) where o.site = site and o.param = param
with
case o.env
when 'Latest' then 1
when 'Stage' then 2
when 'Load' then 3
end as seq,
o.env as env, max(o.dateTime) as dateTime, o.value as value
order by seq
return env, dateTime, value

Just a note, @dan.flavin_enterpris' queries do run.