简体   繁体   English

Cypher 条件`ORDER BY` 子句(相同的属性,不同的 ASC/DESC)

[英]Cypher Conditional `ORDER BY` clause (same property, differ ASC/DESC)

I have two queries:我有两个疑问:

MATCH (n:Node) 
RETURN n.value
ORDER BY n.value DESC
LIMIT 5
MATCH (n:Node) 
RETURN n.value
ORDER BY n.value ASC
LIMIT 5

I would like to combine them both by adding an additional parameter.我想通过添加一个附加参数将它们结合起来。 I tried different approaches with CASE statement, but it looks like the CASE statement allows me to change the property of the sort, not the type of the sort...我用CASE语句尝试了不同的方法,但看起来CASE语句允许我更改排序的属性,而不是排序的类型...

This is a pseudo-code that does what I'm trying to achieve (But this one obviously doesn't work):这是一个伪代码,它完成了我想要实现的目标(但这个显然不起作用):

WITH "ASC" AS sortType
MATCH (n:Node) 
RETURN n.value
ORDER BY n.value (CASE WHEN sortType = "ASC" THEN ASC ELSE DESC END)
LIMIT 5

So the final question is:所以最后的问题是:

  • How can I perform a conditional OrderBy clause on the same property (DESC/ASC difference)?如何对同一属性(DESC/ASC 差异)执行条件OrderBy子句?

You can add a column with a sortValue like this您可以像这样添加带有 sortValue 的列

RETURN  n.value,
        CASE WHEN sortType = ‘DESC’ THEN n.value * -1 ELSE n.value END AS sortValue
ORDER BY sortValue

Adding the sortValue in your RETURN statement makes that you get either在您的 RETURN 语句中添加 sortValue 使您得到

| value | sortValue |
|     1 |          1|
|     2 |          2|
|     3 |          3|

OR要么

| value | sortValue |
|     3 |         -3|
|     2 |         -2|
|     1 |         -1|

You can use this mechanism also in case you want to have flexibility with regard to which column you want to sort, as long as you make sure that you put the right value in the sortValue column.如果您希望对要排序的列具有灵活性,也可以使用此机制,只要您确保在 sortValue 列中放置了正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM