如何使用 Rails 上的 Ruby 为 mySql 组子句中的日期添加别名?
How to alias a Date in a mySql group clause, using Ruby on Rails?
我正在尝试检索开始日期和结束日期之间的设备读数。然后 return 日期数组、计数总和和结果中的 m 个值。但是,当我尝试在我的组子句中为转换后的日期时间设置别名时,我不断收到语法错误。由于我需要将日期时间存储为日期对象,我如何在我的组子句中正确地为其添加别名,以便我可以提取它?
错误:
Mysql2::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 'as date' at line 1: SELECT date, sum(counts) as counts, ANY_VAUE(m) as m FROM device_readings
WHERE device_readings
.device_id = 1 AND (
device_readings.
datetime` BETWEEN '2021-04-01 00:00:00' AND '2021-05-01 00:00:00') GROUP BY DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date)
记录列
datetime: Time object, counts: number, m: number
查询:
readings = @device.readings.
where(:datetime => @start_date..@end_date).
group("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date").
order("datetime ASC").
pluck("date, sum(counts) as counts, ANY_VALUE(m) as m")
您不能将分组依据定义为列并将其用于您的 select,反之亦然
readings = @device.readings.
where(:datetime => @start_date..@end_date).
group("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York'))").
order("datetime ASC").
pluck("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date1 , sum(counts) as counts, ANY_VALUE(m) as m")
您不能在 SQL 中的 GROUP BY 子句中为列设置别名。即使你可以这样做也毫无意义,因为你实际上并没有选择该列,而且你也不能在查询的其他任何地方重复使用它。
readings = @device.readings.
where(datetime: @start_date..@end_date).
group(:date).
order(datetime: :asc).
pluck(
"DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date",
"sum(counts)",
"ANY_VALUE(m)"
)
请注意,如果您使用 .pluck
,则为列设置别名是没有意义的,除非您在查询中引用它们。 pluck 的结果始终是一个数组数组。如果你想要哈希,你想压缩结果:
readings.map {|r| Hash[[:date, :counts, :m].zip(r)] }
你也可以创建 a SQL view 和一个相应的模型,如果你想要花哨的话。
我正在尝试检索开始日期和结束日期之间的设备读数。然后 return 日期数组、计数总和和结果中的 m 个值。但是,当我尝试在我的组子句中为转换后的日期时间设置别名时,我不断收到语法错误。由于我需要将日期时间存储为日期对象,我如何在我的组子句中正确地为其添加别名,以便我可以提取它?
错误:
Mysql2::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 'as date' at line 1: SELECT date, sum(counts) as counts, ANY_VAUE(m) as m FROM
device_readings
WHEREdevice_readings
.device_id= 1 AND (
device_readings.
datetime` BETWEEN '2021-04-01 00:00:00' AND '2021-05-01 00:00:00') GROUP BY DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date)
记录列
datetime: Time object, counts: number, m: number
查询:
readings = @device.readings.
where(:datetime => @start_date..@end_date).
group("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date").
order("datetime ASC").
pluck("date, sum(counts) as counts, ANY_VALUE(m) as m")
您不能将分组依据定义为列并将其用于您的 select,反之亦然
readings = @device.readings.
where(:datetime => @start_date..@end_date).
group("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York'))").
order("datetime ASC").
pluck("DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date1 , sum(counts) as counts, ANY_VALUE(m) as m")
您不能在 SQL 中的 GROUP BY 子句中为列设置别名。即使你可以这样做也毫无意义,因为你实际上并没有选择该列,而且你也不能在查询的其他任何地方重复使用它。
readings = @device.readings.
where(datetime: @start_date..@end_date).
group(:date).
order(datetime: :asc).
pluck(
"DATE(CONVERT_TZ(datetime, 'GMT', 'America/New_York')) as date",
"sum(counts)",
"ANY_VALUE(m)"
)
请注意,如果您使用 .pluck
,则为列设置别名是没有意义的,除非您在查询中引用它们。 pluck 的结果始终是一个数组数组。如果你想要哈希,你想压缩结果:
readings.map {|r| Hash[[:date, :counts, :m].zip(r)] }
你也可以创建 a SQL view 和一个相应的模型,如果你想要花哨的话。