显示选择日期范围内的第一天记录和选定日期范围内的结束日期记录。日期范围可以是任何日期

时间:2020-02-14 10:44:46

标签: sql hana

我需要显示用户输入日期范围内的开放和关闭库存。

对于未平仓交易,应该在选定日期范围的第一天取库存,而对结束收货,应该从选定日期范围的结束日期取库存。

假设我有1月3日至1月30日的数据(在数据库中),并且用户选择了日期范围(1月1日至1月31日)在公开库存中,应显示 1月3日的库存;在未公开库存中,应显示 1月30日的库存。

如何在SAP HANA Studio中做到这一点?

2 个答案:

答案 0 :(得分:0)

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

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xsi:schemaLocation="
         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
         http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext xmlns="http://camel.apache.org/schema/spring">

    <route id="helloRoute">
      <!-- incoming requests from the servlet is routed -->
      <from uri="servlet:hello"/>
      <choice>
        <when>
          <!-- is there a header with the key name? -->
          <header>name</header>
          <!-- yes so return back a message to the user -->
          <transform>
            <simple>Hi I am ${sysenv.HOSTNAME}. Hello ${header.name} how are you today?</simple>
          </transform>
        </when>
        <otherwise>
          <!-- if no name parameter then output a syntax to the user -->
          <transform>
            <constant>Add a name parameter to uri, eg ?name=foo</constant>
          </transform>
        </otherwise>
      </choice>
    </route>

  </camelContext>

</beans>

答案 1 :(得分:0)

以下查询可能会满足您的需求:

SELECT 
 MAX(CASE WHEN S.dateStock = DT.minDate THEN S.dateStock END) AS openDate,
 MAX(CASE WHEN S.dateStock = DT.minDate THEN S.value END) AS openStock,
 MAX(CASE WHEN S.dateStock = DT.maxDate THEN S.dateStock END) AS closeDate,
 MAX(CASE WHEN S.dateStock = DT.maxDate THEN S.value END) AS closeStock
FROM
 stock S
JOIN(
 SELECT 
  min(dateStock) as minDate, 
  max(dateStock) as maxDate
 FROM stock 
 WHERE dateStock BETWEEN '2020-01-01' AND '2020-01-31'
) DT ON S.dateStock = DT.minDate OR S.dateStock = DT.maxDate

SEE DEMO HERE