On Thursday, December 14, 2017 at 6:57:30 AM UTC-8, Gencer W. G. wrote:
>
> What I want to achieve:
>
> WITH summary AS (
>     SELECT f.* ,
>       ROW_NUMBER() OVER(PARTITION BY f.name
>                       ORDER BY f.version DESC) AS rk
>       FROM files f WHERE f.user_id = 1 and f.is_deleted IS FALSE)
>                SELECT s.*
>                  FROM summary s
>                WHERE s.rk = 1"
>
> This query works as a RAW SQL. But I am unable to get f.*, row_number() 
> together on Sequel. I only get row_number() for select. Also a little help 
> on writing this query like above (for surrounding by WITH) is appreciated.
>
> So far tried:
>
> TableModel.select{Sequel[:reserves].* 
> }.select{row_number.function.over(partition: object_name, order: 
> version.desc).as(:rk)}.where(... # i did not finish the query.
>
> How do I make sure f* and row number as rk get together with surrounded 
> WITH on Sequel query builder? Or should I use raw?
>

You don't need to use raw SQL for this:

DB.from{summary.as(:s)}.
  select_all(:s).
  where{s[:rk] =~ 1}.
  with(:summary, DB.from{files.as(:f)}.
    select_all(:f).
    select_append{row_function.function.over(:partition=>f[:name], 
:order=>f[:version].desc).as(:rk)}.
    where{f[:user_id] =~ 1}.
    where{f[:deleted_at] =~ false})

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 post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to