piccolo-orm/piccolo

Be able to extract parts of timestamps / dates

dantownsend opened this issue · 0 comments

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)