首页 > 解决方案 > 显示所选日期范围的第一天和所选日期范围的结束日期的记录。日期范围可以是任何日期

问题描述

我需要显示用户输入日期范围的开盘和收盘库存。

对于未平仓股票,它应该从选定的日期范围内的第一天获取库存,而关闭的股票应该从选定的日期范围内的结束日期获取库存。

假设我有1 月 3 日至 1 月 30 日的数据(在数据库中),并且用户选择了日期范围(1 月 1 日至 1 月 31 日),因此在未平仓库存中应该显示1 月 3 日的库存,在收盘时应该显示1 月 30 日的库存。

如何在 SAP HANA Studio 中执行此操作?

标签: sqlhana

解决方案


猜测源数据的外观。我试图解决你的问题。

with data as (
--here are some example data
select to_date('2019-12-31','yyyy-mm-dd') d ,-1 stock from dual union all
select to_date('2020-01-03','yyyy-mm-dd') d ,1 stock from dual union all
select to_date('2020-01-04','yyyy-mm-dd') d ,2 stock from dual union all
select to_date('2020-01-30','yyyy-mm-dd') d ,3 stock from dual union all
select to_date('2020-02-01','yyyy-mm-dd') d ,2 stock from dual 
)
,filt_data as (
select * from data
where d between to_date('2020-01-01','yyyy-mm-dd') and to_date('2020-01-31','yyyy-mm-dd')
)
, min_date as(
select min(d) d from filt_data
)
, max_date as(
select max(d) d from filt_data
)
select (select d from filt_data where d = (select d from min_date)) open_stock_date
      ,(select stock from filt_data where d = (select d from min_date)) open_stock
      ,(select d from filt_data where d = (select d from max_date)) close_stock_date
      ,(select stock from filt_data where d = (select d from max_date)) close_stock
from  dual ;

推荐阅读