Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting by date a grouped query

I have a data table where I enter all my expenses and incomes.
I would like to group them using the query.

M   1000    income      12/02/2023
b   2000    income      12/03/2023
a   -400    spending    22/03/2023
d   2000    income      14/01/2023
b   -300    spending    12/01/2023
a   1000    income      12/08/2023

The query so far looks like this :

=QUERY(A1:B6;"select A, sum(B), count(B) group by A";)
The issue I have is that I would like to sort the query by date (from the first occurence, for exemple b would be first in row, followed by d then M then a.)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Is there a way I can do that?

https://docs.google.com/spreadsheets/d/13FgeEmMcbiZf_VKRsQ2NzCS9yc8316qNJT6cRl5x_y4/edit?usp=sharing

>Solution :

Within QUERY you could use MIN(D) to find the "smallest" date and sort the range by it:

=QUERY(A1:D;"select A, sum(B), count(B),MIN(D) where A is not null group by A order by MIN(D)";)

enter image description here

If you want, you can wrap it in another QUERY to get rid of that extra column:

=QUERY(QUERY(A1:D;"select A, sum(B), count(B),MIN(D) where A is not null group by A order by MIN(D)";);
"Select Col1,Col2,Col3") 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading