Is there a non-raw-SQL way to do this with Sequel?

SELECT * FROM (SELECT * FROM table WHERE ugly = 2 ORDER BY priority
ASC) AS subquery GROUP BY name HAVING status = 1

I can get the subquery built okay:

db[:table].filter(:ugly => 2).order(:priority.asc)

And if I wrap it:

db.select(db[:table].filter(:ugly =>
2).order(:priority.asc)).group(:name).having(:status=>1)

I'm nearly there.  But MySQL won't execute that without the subquery
named.  It will raise:

Sequel::DatabaseError: Mysql::Error: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near 'GROUP BY `name`...

As a newbie to Sequel, I'm unsure how to use as(). How does one use it
to achieve what I need?

Naively, I first tried:

db.select(db[:table].filter(:ugly =>
2).order(:priority.asc)).as(:subquery).group(:name).having(:status=>1)

As that didn't work, moving the as() to the last appeared to work
until I tried to use .first() on it.  Oops.  It's a
Sequel::SQL::AliasedExpression object and I don't know how to use
those like a data set yet.

Advice?

Aaron out.

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sequel-talk?hl=en.

Reply via email to