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

Add the ability to do DISTINCT ON queries #34

Open
tirsoh opened this issue Sep 21, 2018 · 0 comments
Open

Add the ability to do DISTINCT ON queries #34

tirsoh opened this issue Sep 21, 2018 · 0 comments

Comments

@tirsoh
Copy link

tirsoh commented Sep 21, 2018

Our database is a temporal design because we want to maintain history. So we never update a row and always just insert another row. But at times we want to bring back the most recent record of a given item. We have a digest key that is a hash of a logical key for an item. We then can query by this digest key and say DISTINCT ON digest_key ordered by record_created_on DESC. This gives us the most recent record for that given digest_key.

Table with Temporal Example:

item_id | digest_key | state_code | item_name | price | record_created_on |
1 | 123 | IL | foobar | 12.00 | 09-20-2018 13:45 |
2 | 123 | IL | foobar | 14.00 | 08-20-2018 13:45 |
3 | 123 | IL | foobar | 13.00 | 07-20-2018 13:45 |
4 | 456 | FL | foobar | 16.00 | 09-20-2018 13:45 |
5 | 456 | FL | foobar | 14.00 | 08-20-2018 13:45 |
6 | 456 | FL | foobar | 15.00 | 07-20-2018 13:45 |

When I run the query to get the latest records for each item it would return the following results:

item_id | digest_key | state_code | item_name | price | record_created_on |
1 | 123 | IL | foobar | 12.00 | 09-20-2018 13:45 |
4 | 456 | FL | foobar | 16.00 | 09-20-2018 13:45 |

Maybe there is a way to obtain this result with the current advanced query builder but if there is I would appreciate some instructions on how to get this result. 😄 Otherwise I think this would be a nice feature to add to QueryTree to allow users to do DISTINCT ON type logic.

This is a great tool so far! Keep up the great work!

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

No branches or pull requests

2 participants