Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to extract parts of timestamps / dates #856

Open
dantownsend opened this issue Jul 2, 2023 · 0 comments · May be fixed by #857
Open

Be able to extract parts of timestamps / dates #856

dantownsend opened this issue Jul 2, 2023 · 0 comments · May be fixed by #857
Labels
enhancement New feature or request

Comments

@dantownsend
Copy link
Member

If we have this table:

class Concert(Table):
    band = ForeignKey(Band)
    venue = ForeignKey(Venue)
    starts = Timestamptz()

If we want to get the year that the concert is on, we have to use SelectRaw. We could have it a a builtin feature instead, with an API like this:

# Get the years in which the band has had concerts
>>> await Concert.select(Concert.starts.year).where(Concert.band.name == 'Pythonistas').distinct().output(as_list=True)
[2022, 2023]

It will be most useful if we can make it work with where clauses:

# Get all concerts in certain years
await Concert.select().where(
    Concert.starts.year.is_in([2007, 2009, 2023])
)

And group by:

# Get the number of concerts in each year
await Concert.select(
    Concert.starts.year,
    Count()
).group_by(Concert.starts.year).order_by(Concert.starts.year)
@dantownsend dantownsend added the enhancement New feature or request label Jul 2, 2023
@dantownsend dantownsend added this to To do in Enhancements via automation Jul 2, 2023
@dantownsend dantownsend linked a pull request Jul 2, 2023 that will close this issue
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Development

Successfully merging a pull request may close this issue.

1 participant