【问题标题】:Postgres 'to_char' in Sequel续集中的 Postgres 'to_char'
【发布时间】:2015-04-29 18:23:39
【问题描述】:

我正在使用 Sequel 编写 Sinatra API,但我不知道如何将我的一些 postgres 查询转换为 Sequel。我的表有一个日期列,我想绘制按年月分组的记录,所以我有以下 SQL:

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

这是使用此to_char(date, 'YYYY-MM') 的少数查询之一。我在 Sequel 文档上找不到任何关于此的内容。

【问题讨论】:

标签: ruby postgresql sinatra sequel


【解决方案1】:

to_char是一个SQL函数,在Sequel的文档中有不少地方讨论过它们,比如http://sequel.jeremyevans.net/rdoc/files/doc/sql_rdoc.html#label-Functions

这是您的 SQL 到 Sequel 的 DSL 的翻译:

DB[:receipts].
  distinct.
  select{to_char(:date, 'YYYY-MM').as(:year_month)}.
  where{date >= ?}.
  order(:year_month)

【讨论】:

    猜你喜欢
    • 2012-12-18
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 2021-05-13
    • 2019-10-16
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多