1

What is the proper syntax in Sequel to order by a slice of an SQL array value?

Here's what I want to do in SQL:

SELECT
  a,                -- column with SQL array values
  depth,
  name
FROM
  nodes
ORDER BY
  a[0:depth], name  -- sort by 'a' ignoring last element, then by 'name'

In Sequel, I could do:

DB[:nodes].select(:a, :depth, :name).order(:a, :name)

but this sorts by all of :a, not the [0:depth] slice as desired. What's the proper syntax?

1 Answer 1

1

This works:

DB[:nodes].select(:a, :depth, :name).order('a[0:depth]'.lit, :name)

Any better suggestions?

Sign up to request clarification or add additional context in comments.

1 Comment

That looks like the best way. Sequel's DSL has no support for the [0:depth] syntax. You could do :a.sql_subscript('0:depth'.lit), but that doesn't buy you much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.