I would like to extract the first entry in each group using the SqlAlchemy ORM, but struggle with the construct of this:
Example: Extract the first ever ordered item of each customer:
customer | date | item
------------------------
Joe | 20190101 | duck
Jane | 20190101 | rabbit
Joe | 20190202 | table
Bob | 20190301 | guitar
Jane | 20190301 | guitar
expected output
customer | date | item
------------------------
Joe | 20190101 | duck
Jane | 20190101 | rabbit
Bob | 20190301 | guitar
The problem is solved easily enough for pandas
df.sort_values(['date'],ascending=True).groupby('customer').first().reset_index()
# -- or --
df.groupby(["customer"]).apply(lambda x: x.sort_values(["date"], ascending = True)).first().reset_index()
but i cannot find out how to achieve similar for SqlAlchemy (or for SQL for what it matters, as I struggle to understand the syntax and use of these Partition-Over functions).
Thanks for any help