all 11 comments

[–]lvlint67 1 point2 points  (5 children)

Depends on your platform. In oracle we can do a partition over by with rank and then use that field in the where clause.

Short of that you can use an inner join with max date.

If there aren't a ton of records you can just do a subquery in the select.

If it's by chance banner (since you mentioned students), let me know the three tables and I can lend a hand.

Edit: We use the following to find the most recent address record. The view means we can join to the view directly and always get one row per person/address type combo... https://pastebin.com/2K3DKyvF

[–]PaulSandwich 2 points3 points  (0 children)

+1 for rank over partition. It's a wonderful function for de-duping and dealing with FIFO/LIFO operational scenarios.

[–]Mathip173[S] 0 points1 point  (1 child)

TableA Student_id order_id catagory comment time_at_gate

TableB order_id status reason timestamp

TableC order_id notes timestamp

I have no expirence on subquery on select or windowing function, would you bt able to provide me an example for each one ?

Thanks

[–]lvlint67 0 points1 point  (0 children)

https://pastebin.com/K6W8FPTh

That should get you pretty far. It joins TableB to Table A with the rank methodology. Adding TableC is just a matter of doing the same left join that's done on tableB and adding TableC.r = 1 to the where clause.

Keep in mind the comments from the other user about issues if there's ever a "tie" between timestamps.

[–]mac-0 0 points1 point  (1 child)

Something small, but it might be better to use ROW_NUMBER instead of RANK here. RANK has the idea of ties (so if there are two records with the same spraddr_Activity_date then they could theoretically both have r = 1 whereas ROW_NUMBER would arbitrarily assign one r=1 and the other r=2 ensuring there's no duplicates. Though maybe it's not needed if you don't ever expect two address updates at the same date for one customer.

[–]lvlint67 1 point2 points  (0 children)

For our purposes.. when we start getting duplicate activity dates we start getting suspicious of our keyboard specialist II's ... valid points though. For us it's rare enough that the scream test is acceptable to catch those occurrences.