Trying to select the latest of visit of a customer
I have these schemas:
I'm trying to get the last visit that the customer made but, I can't figure out how to write the query with tanstack db.
My last attempt was joining the two collections and then grouping by customer id and then trying to select biggest (last) visit date but I found that
max
doesn't support dates 🙁
Is there another way to do this?15 Replies
provincial-silverOP•2mo ago
Looking at
max
and min
I think adding support for dates should be easy... at least that what it looks like.stormy-gold•2mo ago
Yep, we need to add support for dates to max/min. You are very welcome to have a go, as you said it should be quite easy. I'll review any PR as soon as you post it.
The proposed query would then work.
provincial-silverOP•2mo ago
I'm looking into but, I noticed that
max
always returns a number
. Is that OK if we pass in a date and, we get back, a number? Or should I override something somewhere to make it return a date?evident-indigo•2mo ago
generally you order by date and limit 1
provincial-silverOP•2mo ago
Well, I want all the customers and I want the latest date for all of them.
I think that approach would work for a single customer, but not when I'm getting multiple.
stormy-gold•2mo ago
Yep, we can change max to return the type that is passed in, for for dates return the max date.
provincial-silverOP•2mo ago
I wonder if I have to do some work to make it work with
gte
and the like?
Right now, I'm pretty sure it doesn't work with my changes since max
and min
now return the date that was passed in.stormy-gold•2mo ago
If you're happy to pick that up too that would be awesome! It should be relatively simple too.
provincial-silverOP•2mo ago
Yep, giving it a shot.
provincial-silverOP•2mo ago
I've got a PR up: https://github.com/TanStack/db/pull/428
GitHub
feat: add support for dates to max and min functions. by MAST1999 ...
I tried not to change too many things.
If something doesn't look right, or we need some tests that are missing, I'm happy to try and make those changes.
BTW, I tried adding date sup...
stormy-gold•2mo ago
Awesome! I'll take a good look at it tomorrow. Thank you.
provincial-silverOP•2mo ago
Just pinging as a reminder
I need a re-review on my PR.
stormy-gold•2mo ago
Hey. Yep, it's not dropped off the radar. We've just been a bit stacked on bug fixes the last couple of weeks. I'm planning to try and get through the backlog of PRs and issues this coming week.
Really appreciate the contribution!
evident-indigo•2mo ago
I also was rude enough to have a baby and go on parental leave so Sam & Kevin have been short handed 😆
provincial-silverOP•2mo ago
No worries, just sending a message as a reminder 🙂
Thanks for all the hard work!