Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/9.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
使用oracle sql计算where子句中日期范围内的运行总计_Sql_Oracle - Fatal编程技术网

使用oracle sql计算where子句中日期范围内的运行总计

使用oracle sql计算where子句中日期范围内的运行总计,sql,oracle,Sql,Oracle,我有一个表,我想用where子句中的日期范围计算运行总数 +----------------+------------------+----------+ | Transaction ID | Transaction Date | Quantity | +----------------+------------------+----------+ | 1 | 03-May-20 | 3 | | 2 | 06-May

我有一个表,我想用where子句中的日期范围计算运行总数

+----------------+------------------+----------+
| Transaction ID | Transaction Date | Quantity |
+----------------+------------------+----------+
|              1 | 03-May-20        |        3 |
|              2 | 06-May-20        |        5 |
|              3 | 05-Jun-20        |       10 |
|              4 | 06-Jul-20        |        2 |
|              5 | 07-Aug-20        |        8 |
+----------------+------------------+----------+
现在,我的oracle sql查询是

select
transaction_id,
transaction_date,
sum(quantity) over (order by transaction_date) as running_total

from table

where transaction_date >= '03-May-20' and transaction_date <= '06-Jul-20'
这是错误的,因为我想要的输出是:

| Transaction ID | Transaction Date | Quantity | Running Total |
+----------------+------------------+----------+---------------+
|              1 | 03-May-20        |        3 |             3 |
|              2 | 06-May-20        |        5 |             8 |
|              3 | 05-Jun-20        |       10 |            18 |
|              4 | 06-Jul-20        |        2 |            20 |
+----------------+------------------+----------+---------------+
请帮助我使用什么查询来计算where子句中包含日期范围的运行总数

您好,我尝试使用to_date函数,但得到了与上面相同的结果(第二张表)。请参见下面的查询

select
transaction_id,
transaction_date,
sum(quantity) over (order by transaction_date) as running_total

from table

where transaction_date between TO_DATE('2019-03-01', 'YYYY-MM-DD') AND TO_DATE('2020-10-25', 'YYYY-MM-DD')
谢谢,

您想要的输出(第1-7行中的示例数据;查询从第8行开始)

你问题的原因是什么?我猜想这是因为您将日期与字符串进行比较(
'03-May-20'
是字符串,而不是日期)。Oracle尝试隐式转换数据类型;有时成功,有时失败。例如,您的
03-May-20
可能是2020年5月3日或2003年5月20日

始终控制您的数据。必要时使用日期,可以使用带有适当日期格式掩码的“to_date”函数,也可以使用日期文字(如我所做)。

您的查询是“正确”的,但结果不是,它们包含预期的运行总数,但它从最终总数(包括事务id 5)开始。您运行了不同的SQL语句


请注意,您不应该将日期存储为字符串或将其与字符串进行比较。也就是说,如果您当前的NLS日期格式设置(很容易更改)与您的字符串输入匹配,Oracle将保存您的bacon。如果按字符串排序,您将得到完全不同的结果,您的结果具有预期的事务日期值,因此可以假定此比较有效。您仍然应该修复日期处理,但这不是问题所在。

发生了其他事情。数据按日期正确汇总,因此日期排序似乎不是问题

总和从总和减去第一个值开始。因此,结果类似于此查询的结果:

select transaction_id, transaction_date, quantity,
       total_quantity + sum(quantity) over (order by transaction_date)as running_total
From (select t.*,
             sum(quantity) over () - first_value(quantity) over (order by transaction_date) as total_quantity
      from test t
     ) t
where transaction_date between date '2020-05-03' and date '2020-07-06'
order by transaction_date;

我无法解释为什么会发生这种情况。这可能是Oracle中的一个bug。或者,这可能是您在提出问题时过于简化查询的结果。

我不知道您的数据如何生成您指定的数据。数量之和大于表中显示的数量。
SQL> with test (transaction_id, transaction_date, quantity) as
  2    (select 1, date '2020-05-03',  3 from dual union all
  3     select 2, date '2020-05-06',  5 from dual union all
  4     select 3, date '2020-06-05', 10 from dual union all
  5     select 4, date '2020-07-06',  2 from dual union all
  6     select 5, date '2020-08-07',  8 from dual
  7    )
  8  select transaction_id,
  9    transaction_date,
 10    quantity,
 11    sum(quantity) over (order by transaction_date) running_total
 12  From test
 13  where transaction_date between date '2020-05-03' and date '2020-07-06'
 14  order by transaction_id;

TRANSACTION_ID TRANSACTIO   QUANTITY RUNNING_TOTAL
-------------- ---------- ---------- -------------
             1 03.05.2020          3             3
             2 06.05.2020          5             8
             3 05.06.2020         10            18
             4 06.07.2020          2            20

SQL>
select transaction_id, transaction_date, quantity,
       total_quantity + sum(quantity) over (order by transaction_date)as running_total
From (select t.*,
             sum(quantity) over () - first_value(quantity) over (order by transaction_date) as total_quantity
      from test t
     ) t
where transaction_date between date '2020-05-03' and date '2020-07-06'
order by transaction_date;