我试图根据另一个表中的值从日期列中减去分钟数。但是查询会出错。有人可以就此提出建议吗?
TABLE 1 NAME - MYTABLE1
COLUMN NAME "IN_DATE" Data type - DATE
TABLE 2
NAME - CONFIG_TABLE
COLUMN NAME - PARAM_NAME VALUE = SUBTRACT_MINUTE_VALUE
COLUMN NAME PARAM_VALUE VALUE =15
SELECT IN_DATE , IN_DATE - interval (SELECT PARAM_VALUE FROM CONFIG_TABLE WHERE PARAM_NAME='SUBTRACT_MINUTE_VALUE') minute FROM MYTABLE1
答案 0 :(得分:4)
另一种选择是使用一分钟的interval
并将其与存储在配置表中的数字相乘。
select m.in_date,
m.in_date - (interval '1' minute * c.param_value) as result
from mytable1 m
cross join config_table c on c.param_name = 'SUBTRACT_MINUTE_VALUE'
答案 1 :(得分:3)
以下是我的操作方法:将分钟数减去分钟数除以(24小时(一天)x60分钟(一小时))
SQL> alter session set nls_date_format = 'dd.mm.yyyy hh24:mi';
Session altered.
SQL> create table config (subtract_minute_value number);
Table created.
SQL> insert into config values (15);
1 row created.
SQL> create table mytable1 (in_date date);
Table created.
SQL> insert into mytable1 values (sysdate);
1 row created.
SQL>
SQL> select m.in_date, m.in_date - c.subtract_minute_value / (24 * 60) result
2 from mytable1 m, config c;
IN_DATE RESULT
---------------- ----------------
02.02.2018 06:56 02.02.2018 06:41
SQL>
[编辑基于Aleksej的ANSI JOIN建议,以及NUMTODSINTERVAL选项]
SQL> select m.in_date,
2 m.in_date - c.subtract_minute_value / (24 * 60) result
3 from mytable1 m join config c on 1 = 1;
IN_DATE RESULT
---------------- ----------------
02.02.2018 07:51 02.02.2018 07:36
SQL>
SQL> select m.in_date,
2 m.in_date - numtodsinterval(c.subtract_minute_value, 'minute') result
3 from mytable1 m join config c on 1 = 1;
IN_DATE RESULT
---------------- ----------------
02.02.2018 07:51 02.02.2018 07:36
SQL>
答案 2 :(得分:0)
你也可以试试这个,
ALTER SESSION SET NLS_DATE_FORMAT = 'MM/DD/YYYY HH:MI:SS AM';
SELECT SYSDATE current_time,
SYSDATE - (1/24/60)*:p_min current_time_minus_mins
FROM DUAL;