【发布时间】:2014-09-19 09:24:24
【问题描述】:
我有一个这样的查询。
select
ad.escore,
ad.mscore,
round(sum(ps.cnt) / sum(n.cnt) * 100,1) as percent
from
(
select
account_no,
-- 602 becomes '595-604'
to_char(trunc(empirica_score - 5, -1) + 5, '9999') || '-' || to_char(trunc(empirica_score - 5, -1) + 14, '9999') as escore,
-- 97 becomes '76-100'. Change the expression to group differently.
cast(((mfin_score - 1) / 25) * 25 + 1 as text) || '-' || cast(((mfin_score - 1) / 25) * 25 + 25 as text) as mscore
from account_details
) ad
join
(
select custno, count(*) as cnt
from paysoft_results
where result = 'Successful'
and resultdate >= '13/08/2014' <------- HERE
and resultdate <= '12/19/2014' <------- HERE
group by custno
) ps on ps.custno = ad.account_no
join
(
select customer_code, count(distinct start_date) as cnt
from naedo
and start_date >= '13/08/2014' <------- HERE
and start_date <= '12/19/2014' <------- HERE
group by customer_code
) n on n.customer_code = ad.account_no
group by ad.escore, ad.mscore;
如果我没有像上面那样安装日期,它就完美了。
如果我输入日期,我会收到错误ERROR: syntax error at or near "and"
有什么想法吗?
更新
好的,我想我可以问一个现在的问题,所以如果我可以追加这个问题。
ERROR: date/time field value out of range: "13/08/2014"
我的查询中的日期比较。正确的做法是什么?
【问题讨论】:
-
早上,更新可能应该是一个新的问题,但我已经在我原来的答案的旁边提到了它。
-
不要不依赖隐式数据类型转换。始终使用正确的日期文字,而不是字符串常量。
'13/08/2014'是一个字符串,而不是日期。您应该使用 Oracle 的 to_date() 函数:to_date('13/08/2014', 'DD/MM/YYYY')或(稍微短一点)ANSI 日期文字:date '2014-08-13'。
标签: sql postgresql