更新:我正在尝试将mysql代码翻译成postgres代码,我想在where子句中的两个日期之间进行过滤
原始Mysql
SELECT concat(month(DD1.open_date),'/', DATE_FORMAT(DD1.open_date, '%y')) AS month_year,
venues.name AS venue_name,
DD1.`server_name` AS SERVER_NAME,
DD1.`server_id` AS `Server ID`,
AVG(lr.`rating`) AS server_overall_rating,
all_overall_rating,
AVG(lr.`service`) AS server_service_rating,
all_service_rating,
avg(spend) AS server_check_avg,
all_check_avg,
avg(cover_avg) AS server_cover_avg,
all_cover_avg
FROM loyalty_visits DD1
JOIN
(SELECT avg(lr.rating) AS all_overall_rating,
avg(lr.service) AS all_service_rating,
avg(cover_avg) AS all_cover_avg,
avg(spend) AS all_check_avg,
open_date
FROM loyalty_visits lv
LEFT OUTER JOIN loyalty_ratings lr ON lr.loyalty_visit_id = lv.id
WHERE lv.venue_id = 4046
AND open_date BETWEEN date_sub(now(), interval 12 MONTH) AND now()
GROUP BY lv.venue_ID,
MONTH(open_date), year(open_date)) AS overall_cover_avg ON MONTH(overall_cover_avg.open_date) = MONTH(DD1.open_date)
AND year(overall_cover_avg.open_date) = year(DD1.open_date)
LEFT OUTER JOIN loyalty_member_visits lmv ON lmv.loyalty_visit_id = DD1.id
LEFT OUTER JOIN loyalty_ratings lr ON lmv.loyalty_visit_id = lr.loyalty_visit_id
JOIN venues ON venues.id = DD1.venue_id
WHERE DD1.parent_venue_id = 4041
AND DD1.server_id = 6991
AND DD1.venue_id = 4046
AND DD1.open_date BETWEEN date('2015-01-29 14:15:55') AND date('2013-09-29 10:15:55')
GROUP BY DD1.venue_ID,
MONTH(DD1.open_date), year(DD1.open_date)
ORDER BY DD1.open_date LIMIT 6
的Postgres 注意:我知道我的投影与原始的mysql相比并不是全部,因为我实际上是在尝试理解subquerying(我不知道它是如何工作的)并且还试图了解一些select语句的来源来自mysql版本,所以我可以充分转换为Postgres。
同时
忠诚度调用因为Mysql中的DD1被改为member_visits,因为在Postgres中的mv和loyalty_ratings,因为lr被改为在Postgres中仅被评为r。其余的应该是一样的。SELECT mv.check_number,
mv.open_date,
mv.code,
mv.cover_avg,
reservation_loyalty_user_id,
v.restaurant_id,
table_name,
terminal_id
FROM member_visits as mv
FROM member_visits as mv
JOIN
(SELECT avg(lr.rating) AS all_overall_rating,
avg(lr.service) AS all_service_rating,
avg(cover_avg) AS all_cover_avg,
avg(spend) AS all_check_avg,
open_date
FROM loyalty_visits lv
LEFT OUTER JOIN ratings lr ON lr.loyalty_visit_id = lv.id
WHERE lv.venue_id = 4046
AND open_date BETWEEN date_sub(now(), interval 3 MONTH) AND now()
GROUP BY lv.venue_ID,
MONTH(open_date), year(open_date)) AS overall_cover_avg ON MONTH(overall_cover_avg.open_date) = MONTH(DD1.open_date)
AND year(overall_cover_avg.open_date) = year(DD1.open_date)
LEFT JOIN ratings r
ON mv.loyalty_visit_id = r.loyalty_visit_id
JOIN venues v
ON v.id = mv.venue_id
WHERE mv.parent_venue_id = 4041
AND mv.server_id = 6991
AND mv.venue_id = 4046
AND mv.open_date BETWEEN date_trunc('2015/01/20', day) AND date_trunc('2013/09/29', day)
GROUP BY mv.venue_ID,
MONTH(mv.open_date), YEAR(mv.open_date)
ORDER BY mv.open_date
limit 10
我遇到两个错误:
1)在这一行
AND open_date BETWEEN date_sub(now(), interval 12 MONTH) AND now()
,
从现在到12个月之间是否有可能没有数据?我不确定如何调试它。
2)我也得到一个错误
invalid input syntax for integer: "2015-01-2013 14:15:55"
我看过这篇文章Filter by Dates in SQL,但我不确定我喜欢这个解决方案,而且它是一个SQL解决方案。然后我尝试使用date_trunc
,但我不确定这是否正确。
我应该使用的任何东西?
答案 0 :(得分:3)
There is no date_sub()
函数。只需从时间戳(或日期)中减去间隔。您的间隔文字也是错误的。
SQL标准要求将值放在单引号中,例如interval '12' month
或interval '5' minute
。 Postgres还允许使用语法,其中值和单位采用单引号interval '1 month'
AND open_date BETWEEN now() - interval '12' MONTH AND now()
您的date_trunc
使用情况is also wrong。字符串文字'2015/01/20'
只会转换为当前日期格式与此匹配的日期。一般来说,使用ANSI日期文字来确保您的SQL在每个环境中都运行,无论日期格式设置如何都 更好
date_trunc('day', DATE '2015-01-20')
Postgres也(总是)接受使用ISO格式的字符串文字,因此'2015-01-20'
的工作方式与Postgres中DATE '2015-01-20'
的工作方式相同
Postgres中也没有date()
功能。我建议使用ANSI日期和时间戳文字。因此,date('2015-01-29 14:15:55')
使用符合SQL标准的timestamp '2015-01-29 14:15:55'
而不是to_date()
。如果您不想使用ISO日期格式,则应使用to_timestamp()
或date_format
functions with an appropriate format mask。
Postgres中也没有to_char()
功能。使用year()
。再次is documented in the manual
Postgres中也没有month()
或extract(year from open_date)
功能。使用SQL标准:extract(month from open_date)
或date_part()
或Postgres特定的{{1}}函数。这也是documented in the manual