Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New type of restriction: top rows. #1024

Closed
dimitri-yatsenko opened this issue May 13, 2022 · 1 comment
Closed

New type of restriction: top rows. #1024

dimitri-yatsenko opened this issue May 13, 2022 · 1 comment
Assignees
Labels
Milestone

Comments

@dimitri-yatsenko
Copy link
Member

dimitri-yatsenko commented May 13, 2022

Feature Request

Problem

Currently, DataJoint does not allow restricting a query to the top entries according to some sorting except for the final fetch operation.

top_students = Student.fetch(order_by='grade desc', limit=10)

This is equivalent to SQL

SELECT * FROM student ORDER BY grade desc LIMIT 10

In many cases it is useful to perform this type of restriction inside an intermediate query.

Requirements

I propose a new restriction type, dj.Top().

For example, the query for top students would look like:

top_students = Student & dj.Top('grade desc', 10)

dj.Top would take the order_by attributes, the limit, and the offset.

Justification

dj.Top allows for new query types that are possible in SQL but have not been easily produced in DataJoint.

For example, the query list the majors for top students:

top_students = Student & dj.Top('grade desc', 10)
StudentMajor & top_students

Alternative Considerations

The operation above could have been done the old way as

top_students = Student.fetch("KEY", order_by='grade desc', limit=10)
StudentMajor & top_students

However, this is two separate queries and the new dj.Top allows producing the result as a single query.

@dimitri-yatsenko dimitri-yatsenko added this to the DataJoint 1.0 milestone Oct 7, 2022
@A-Baji A-Baji self-assigned this May 8, 2023
@A-Baji A-Baji linked a pull request May 17, 2023 that will close this issue
@dimitri-yatsenko
Copy link
Member Author

This was solved with #1084, #1178

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants