条款Oracle

时间:2017-11-30 07:31:38

标签: sql oracle oracle12c

假设我有2种类型的顾客Regular - R和' Corporate - C`的杂货店,我与他们签订了基于日期的价格协议。样本数据看起来像。

Type(C/R)   CustID  From Date   Cost
C                   1/11/2017   10
C              1    1/11/2017   12
                    1/11/2017   14
R                   1/11/2017   9
C              1    10/11/2017  11
C                   11/11/2017  15

从表格中可以看出TypeCustid不是强制性的。我的费率选择器根据日期匹配来自输入的最大匹配列,以便为我提供申请费用。

示例输入:(输入将始终具有类型,custid和日期)

Case 1: Type - c,Cust ID - 1, dealdate(fromdate) - 2/11/2017

输出: Row number 2 with price 12

Case 2: Type - C, Cust ID - 2,dealdate(fromdate) - 2/11/2017

输出: Row number 1 with price 10

Case 3: Type - C, Cust ID - 2,dealdate(fromdate) - 12/11/2017

输出: Row number 6 with price 15

我的输出将首先匹配最大匹配列,然后检查有效日期,这意味着匹配列的优先级高于日期(但是它必须是有效期)。

我的方法:

select * from (
select row_number() over(partition by partition_column order by
from_date desc,type,custid) rn,a.* from (
select *,'1' as partition_column from rate 
where from_date <= :d_date and (type = :type or type is null) and 
(custid = :custid or custid is null)) a) where rn=1;

我没有得到理想的结果。任何人都可以帮忙。

1 个答案:

答案 0 :(得分:1)

好的,我想我明白你的意思。

public class TestSteps implements En {

public TestSteps() {
    Given("some scenario", () -> { /* implement me! */});
    When("some step contains some doc string", () -> { /* implement me! */});
    /* implement me */
    Then("the content of that doc string should be included in the Serenity test report", this::showStepMessage);
}

public void showStepMessage(String message) {
    // TODO: escape message string for use in HTML
    String escapedMessage = StringUtils.replace(message, " ", "&nbsp;");

    StepEventBus.getEventBus().stepStarted(ExecutedStepDescription.withTitle(escapedMessage));
    StepEventBus.getEventBus().stepFinished();
}

此声明的工作原理是查找与类型或客户匹配的记录。必须满足日期输入。最后,客户ID的优先级高于客户类型,如果有多个记录,则会选择最近的日期记录。

create table rate(
   type      varchar2(5)
  ,cust_id   number
  ,from_date date not null
  ,cost      number not null
);

insert into rate(type, cust_id, from_date, cost) values('C',  null, date '2017-11-01', 10);
insert into rate(type, cust_id, from_date, cost) values('C',  1,    date '2017-11-01', 12);
insert into rate(type, cust_id, from_date, cost) values(null, null, date '2017-11-01', 14);
insert into rate(type, cust_id, from_date, cost) values('R',  null, date '2017-11-01',  9);
insert into rate(type, cust_id, from_date, cost) values('C',  1,    date '2017-11-10', 11);
insert into rate(type, cust_id, from_date, cost) values('C',  null, date '2017-11-11', 15);

http://thucydides.info/docs/serenity-staging/#_testing_rest_with_serenity_bdd