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
modern-tealOP•4mo ago
Looking at
max and min I think adding support for dates should be easy... at least that what it looks like.fascinating-indigo•4mo 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.
modern-tealOP•4mo 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?xenial-black•4mo ago
generally you order by date and limit 1
modern-tealOP•4mo 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.
fascinating-indigo•4mo ago
Yep, we can change max to return the type that is passed in, for for dates return the max date.
modern-tealOP•4mo 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.fascinating-indigo•4mo ago
If you're happy to pick that up too that would be awesome! It should be relatively simple too.
modern-tealOP•4mo ago
Yep, giving it a shot.
modern-tealOP•4mo 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...
fascinating-indigo•4mo ago
Awesome! I'll take a good look at it tomorrow. Thank you.
modern-tealOP•3mo ago
Just pinging as a reminder
I need a re-review on my PR.
fascinating-indigo•3mo 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!
xenial-black•3mo ago
I also was rude enough to have a baby and go on parental leave so Sam & Kevin have been short handed 😆
modern-tealOP•3mo ago
No worries, just sending a message as a reminder 🙂
Thanks for all the hard work!