question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow function results in group by

See original GitHub issue

Allow grouping by function result. This is especially useful with datetime fields. A simple example from postgresql :

SELECT EXTRACT(YEAR from date_column) FROM sales group by EXTRACT(YEAR from date_column)

You can see how this is useful when creating for example yearly, monthly or daily sales results. Without this functionality you need to denormalize your table so year , month and day stays in separate columns.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
long2icecommented, Jan 12, 2021

OK, this should be a new feature

0reactions
MrAngrycommented, Jan 12, 2021

I could also add code for ProductModel and District but I think it is not important as those fields do not play any role here. And trying to use annotation does not work as well:

sales= Sales.all()
sales = await sales.annotate(year=Extract(F('date'), 'YEAR')).group_by('year').values('year')

tortoise.exceptions.FieldError: Unknown field "year" for model "Sales"

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL GROUP BY and HAVING Clause with Examples - Guru99
The GROUP BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY clause...
Read more >
Why Aren't SQL Window Functions Allowed in GROUP BY?
It's true – the SQL order of operations means window functions aren't allowed in GROUP BY. But you can use them anyway with...
Read more >
SQL GROUP BY - Everything You Need To Know - Sisense
Group by is one of the most frequently used SQL clauses. It allows you to collapse a field into its distinct values.
Read more >
SQL GROUP BY and HAVING: The Complete Guide
The GROUP BY clause is a clause in the SELECT statement. It allows you to create groups of rows that have the same...
Read more >
SQL GROUP BY to Summarize and Rollup Query Results
To filter the GROUP BY results, you must use the HAVING clause after the GROUP BY. The GROUP BY statement is often used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found