On Thu, Jul 29, 2021 at 11:59 PM Michal Macejko <[email protected]>
wrote:

> nodes.select{|o| [o.members[:id].as(:_id)] +
> [Sequel[:member_integrations][:id]]}.distinct{members[:id]}.from_self.select(:_id).limit(50).count
>
> fails on ERROR:  SELECT DISTINCT ON expressions must match initial ORDER
> BY expressions (Sequel::DatabaseError)
>

Looks like you just need to prepend the DISTINCT ON expression to the ORDER
BY

nodes.select{|o| [o.members[:id].as(:_id)] +
[Sequel[:member_integrations][:id]]}.
  distinct{members[:id]}.
  order_prepend{members[:id]}.
  from_self.select(:_id).
  limit(50)

Not sure if i fully understand the idea. When i take these ordered 600 rows
> and i need to limit them, subselect DISTINCT needs to be ordered by
> member_id first, which means that the ordered 600 rows would be limited by
> unordered 50 members.
>

The whole setup is:

nodes = current_account.members_dataset.eager_graph(member_integrations:
[:member_integration_fields]).order(*array_order)
subselect = nodes.select{|o| [o.members[:id].as(:_id)] +
[Sequel[:member_integrations][:id]]}.
  distinct{members[:id]}.
  order_prepend{members[:id]}.
  from_self.select(:_id).
  limit(50)
nodes.where{|o| o.members[:id] =~ subselect}

The subselect is only used for filtering to the first 50 members.  It's
true that the IN is unordered, but that shouldn't matter in this case.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSSf2_B%2BcsgcjEW9h6dKpiDSnOsje9%3DxgnROGacY%3DDLz_YQ%40mail.gmail.com.

Reply via email to