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

Open
dimitri-yatsenko opened this issue May 13, 2022 · 0 comments · May be fixed by #1084
Open

New type of restriction: top rows. #1024

dimitri-yatsenko opened this issue May 13, 2022 · 0 comments · May be fixed by #1084
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.

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