sql - 显示所选日期范围的第一天和所选日期范围的结束日期的记录。日期范围可以是任何日期
问题描述
我需要显示用户输入日期范围的开盘和收盘库存。
对于未平仓股票,它应该从选定的日期范围内的第一天获取库存,而关闭的股票应该从选定的日期范围内的结束日期获取库存。
假设我有1 月 3 日至 1 月 30 日的数据(在数据库中),并且用户选择了日期范围(1 月 1 日至 1 月 31 日),因此在未平仓库存中应该显示1 月 3 日的库存,在收盘时应该显示1 月 30 日的库存。
如何在 SAP HANA Studio 中执行此操作?
解决方案
猜测源数据的外观。我试图解决你的问题。
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 ;
推荐阅读
- kiba-etl - 如何过滤提取器中的数据?
- bluetooth - 如何在 Android Comms Test Suite 中启用 BtSnoop 日志
- python - 用于识别序列数据的 CNN 模型的配置 - CNN 顶部的架构 - 并行层
- c - 为什么浮点数不会溢出到无穷大
- javascript - 我在 discord.js 中使用 API 时遇到问题
- javascript - 调整窗口大小时,svelte/sapper 中的 InvalidCharacterError
- swift - 使用 SwiftUI 和 Core Data 镜像 CloudKit 进行屏幕更新
- python - 如何使用python在机器人框架中导入和使用用户定义的枚举类
- ios - 从 Bitbucket 管道中的 Ionic 4 项目构建 iOS .ipa
- python - 如何在 tensorflow/keras 中使用常量滤波器执行卷积