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

How to get Aggregate and Stats on a given column in a collection? #169

Open
vtharmalingam opened this issue Feb 27, 2024 · 3 comments
Open

Comments

@vtharmalingam
Copy link

Hi Greetings!

Here is the struct of my collection:

    struct commission_ledger {
        realm::primary_key<realm::object_id> _id;
        int64_t ts;
        int64_t ticket;
        double amount;
    };
    REALM_SCHEMA(commission_ledger, _id, ts, ticket, account)

I need to find the last timestamp when the commission was processed. Currently, I use this kind of code, but I think it is inefficient.

	auto recs = realm_instance->objects<commission_ledger>();

	int64_t ts = 0;
	for (auto& rec : recs) {
		if (rec.ts > ts)
			ts = rec.ts;
	}

Is there a direct "aggregate" syntax possible to be used here and in similar other cases?

Thanks,
Tharma

Copy link

sync-by-unito bot commented Feb 27, 2024

➤ PM Bot commented:

Jira ticket: RCPP-51

@kneth
Copy link
Contributor

kneth commented Mar 6, 2024

You could do realm_instance->objects<commission_ledger>().sort("ts", false) and use the first element of the result.

@vtharmalingam
Copy link
Author

Wonderful, thank you very much! This assistance is truly invaluable. Could you provide any guidance on where I might find documentation covering concepts like "groupby" and others similar to "sort"? Thanks, in advance!

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