Sorting data using the ORDER BY clause
We can sort the results using the ORDER BY
clause. Let us take the drug prescription query and apply sorting to it. First, let us take a look at the query that sorts results by the number of patients to which they are prescribed:
MATCH (d:Drug)<-[:HAS_DRUG]-()<-[:HAS_ENCOUNTER]-(p) WITH DISTINCT d, p RETURN d.description as drug, count(p) as patients ORDER BY patients
This query returns the drug prescriptions in ascending order based on the number of patients they are prescribed to.
Figure 5.13 – Drug prescriptions ordered by the number of patients they are prescribed to
We can see the data in ascending order based on the number of patients the drugs are prescribed to. Now, let us look at the query where the data is in descending order:
MATCH (d:Drug)<-[:HAS_DRUG]-()<-[:HAS_ENCOUNTER]-(p) WITH DISTINCT d, p RETURN d.description as drug, count(p) as patients ORDER BY patients DESC
Let us take...