我使用的是 Oracle SQL,该表中的 tradedate 列的类型为 DATE。我想定义一个 current_date ,每次我都会手动输入一个特定日期。然后,我需要计算一定时间范围内(本例为30天)内的一些指标,例如平均值、标准差等。我哪里出错了? 我尝试了一些版本,但收到错误消息,例如“无效的 SQL 语句”、“错误:ORA-01008:未绑定所有变量”、“Oracle 数据库错误:ORA-01858:在需要数字的地方发现非数字字符” '。 任何指导都会很棒!
DEFINE current_date = TO_DATE('08-Mar-2024', 'DD-Mon-YYYY')
SELECT
r.DVPCustodianID,
COUNT(r.TYPE) AS count_of_eventType,
sub.avg_30_count_of_eventType,
sub.stddev_30_count_of_eventType,
sub.max_30_count_of_eventType,
sub.min_30_count_of_eventType
FROM
DNA_CAT_GWIM.REPORTABLEORDEREVENTS r
LEFT JOIN
(
SELECT
r1.DVPCustodianID,
AVG(CASE WHEN r1.tradedate BETWEEN ¤t_date - 30 AND ¤t_date THEN r1.cnt END) AS avg_30_count_of_eventType,
STDDEV(CASE WHEN r1.tradedate BETWEEN ¤t_date - 30 AND ¤t_date THEN r1.cnt END) AS stddev_30_count_of_eventType,
MIN(CASE WHEN r1.tradedate BETWEEN ¤t_date - 30 AND ¤t_date THEN r1.cnt END) AS min_30_count_of_eventType,
MAX(CASE WHEN r1.tradedate BETWEEN ¤t_date - 30 AND ¤t_date THEN r1.cnt END) AS max_30_count_of_eventType
FROM
(
SELECT
DVPCustodianID,
tradedate,
COUNT(TYPE) AS cnt
FROM
DNA_CAT_GWIM.REPORTABLEORDEREVENTS
GROUP BY
DVPCustodianID, tradedate
) r1
GROUP BY
r1.DVPCustodianID
) sub ON r.DVPCustodianID = sub.DVPCustodianID
WHERE
r.tradedate = TO_DATE('¤t_date', 'DD-Mon-YYYY')
GROUP BY
r.DVPCustodianID,
sub.avg_30_count_of_eventType,
sub.stddev_30_count_of_eventType,
sub.max_30_count_of_eventType,
sub.min_30_count_of_eventType;
我尝试了一些版本,但收到错误消息,例如“无效的 SQL 语句”、“错误:ORA-01008:未绑定所有变量”、“Oracle 数据库错误:ORA-01858:在数字所在的位置发现非数字字符”预期的'。任何指导都会很棒
将变量定义为字符串;省略函数调用 - 当将字符串转换为有效的日期值时,在查询中使用它。
例如:
SQL> select min(hiredate) from emp;
MIN(HIREDA
----------
17.12.1980
SQL> define my_date = '17.12.1980' --> no TO_DATE function here
SQL> select empno, ename, job, sal
2 from emp
3 where hiredate = to_date('&my_date', 'dd.mm.yyyy'); --> TO_DATE is here
old 3: where hiredate = to_date('&my_date', 'dd.mm.yyyy')
new 3: where hiredate = to_date('17.12.1980', 'dd.mm.yyyy')
EMPNO ENAME JOB SAL
---------- ---------- --------- ----------
7369 SMITH CLERK 800
SQL>