0

I'm writing a Sinatra API using Sequel, but I don't know how to translate some of my postgres queries into Sequel. My table has a date column and I want to graph records grouped by year-month, so I have the following SQL:

year_months_sql = "select distinct to_char(date, 'YYYY-MM') as year_month
from receipts
where date >= ?
order by year_month asc"

This is one of a few queries that uses this to_char(date, 'YYYY-MM'). I can't find anything on Sequel docs about this.

1

1 Answer 1

3

to_char is an SQL function, and there are quite a few places in Sequel's documentation that discuss them, such as http://sequel.jeremyevans.net/rdoc/files/doc/sql_rdoc.html#label-Functions

Here's a translation of your SQL to Sequel's DSL:

DB[:receipts].
  distinct.
  select{to_char(:date, 'YYYY-MM').as(:year_month)}.
  where{date >= ?}.
  order(:year_month)
Sign up to request clarification or add additional context in comments.

Comments

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.