Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 获取日期之间的数据使用_Sql_Postgresql - Fatal编程技术网

Sql 获取日期之间的数据使用

Sql 获取日期之间的数据使用,sql,postgresql,Sql,Postgresql,我需要查询以获取数据成本\ u月/月,如下所示: select sum(xscf_cost_month) as cost_month from ncloud_data_service_cof a join ncloud_data_ba b on b.xcba_cof_no = a.xscf_cof_no where to_time('2014-05', 'YYYY-mm') between to_char(b.xcba_activation_date, 'YYYY-mm') and to_cha

我需要查询以获取数据成本\ u月/月,如下所示:

select sum(xscf_cost_month) as cost_month
from ncloud_data_service_cof a
join ncloud_data_ba b on b.xcba_cof_no = a.xscf_cof_no
where to_time('2014-05', 'YYYY-mm') between to_char(b.xcba_activation_date, 'YYYY-mm') and to_char(b.xcba_end_activation_date, 'YYYY-mm')
上面的查询显示错误: 错误:运算符不存在:日期>=文本 第4行:截止日期('2014-05','YYYY-mm')在至字符(b.xcba)之间

如何修复它?我尝试用to_时间更改为_char,但没有得到解决方案。 请帮帮我你打算这样做吗

select sum(xscf_cost_month) as cost_month
from ncloud_data_service_cof a join
     ncloud_data_ba b
     on b.xcba_cof_no = a.xscf_cof_no
where '2014-05' between to_char(b.xcba_activation_date, 'YYYY-mm') and 
                        to_char(b.xcba_end_activation_date, 'YYYY-mm');

您不需要转换字符串
'2014-05'
。它已经是一个字符串。

此字符串仍然可以在b.xcba\u activation\u date和b.xcba\u end\u activation\u date列上使用索引:

SELECT sum(xscf_cost_month) as cost_month
FROM ncloud_data_service_cof a 
     JOIN
         ncloud_data_ba b ON b.xcba_cof_no = a.xscf_cof_no
WHERE to_time('2014-05', 'YYYY-mm') BETWEEN date_trunc('month', b.xcba_activation_date) AND date_trunc('month', b.xcba_end_activation_date);

转换为字符串会使日期索引无效。最好将输入转换为相同的数据类型。