Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group by and order by in cosmos db?

As this doc said, "You currently cannot use GROUP BY with an ORDER BY clause but this is planned". But we do need to group by one field and order by another field. Is there a way to do it?

like image 336
Peterxwl Avatar asked Nov 06 '22 11:11

Peterxwl


1 Answers

According to my research, i'm afraid that there is no such direct official way to use GROUP BY with ORDER BY since the statement you mentioned in your question:

The GROUP BY clause must be after the SELECT, FROM, and WHERE clause and before the OFFSET LIMIT clause. You currently cannot use GROUP BY with an ORDER BY clause but this is planned.

You could submit your feedback to push the progress of this feature.

If your need is urgent,i would suggest you :

  • a:sort the date after group by.Such as ARRAY.SORT() method in .net code.
  • b:Or you could group by the data with this package(which is built on Stored Procedure in cosmos db) after order by.
like image 143
Jay Gong Avatar answered Nov 29 '22 01:11

Jay Gong