转换范围记录

时间:2018-01-19 20:15:07

标签: sql hana

我有以下场景,需要一些SQL帮助

onCellPrepared: function(e) {
            if(e.rowType === "data" && e.column.command === "edit") {
                var isEditing = e.row.isEditing,
                    $links = e.cellElement.find(".dx-link");

                $links.text("");

                if(isEditing){
                    $links.filter(".dx-link-save").addClass("dx-icon-save");
                    $links.filter(".dx-link-cancel").addClass("dx-icon-revert");
                } else {
                    $links.filter(".dx-link-edit").addClass("dx-icon-edit");
                    $links.filter(".dx-link-delete").addClass("dx-icon-trash");
                }
            }
        }

我需要像这样的结果

ID  Flag    ValidFrom     Valid To
1    A      2017001       2017005   ( Valid Till end of 2017004)
1    B      2007005       2017008
1    C      2017008       2017012
2    D      2017001       2017006
2    E      2007006       2017008
2    F      2017008       2017012

实际上我只需要最少3个月,所以我实际上需要这样的东西

1      2017001    A
1      2007002    A
1      2017003    A
1      2017004    A
1      2007005    B
1      2017006    B
1      2017007    B
1      2007008    C
1      2017009    C
1      2017010    C
1      2017011    C
2      2017001    D
2      2007002    D
2      2017003    D
2      2017004    D
2      2007005    D
2      2017006    E
2      2017007    E
2      2007008    F
2      2017009    F
2      2017010    F
2      2017011    F

提前致谢

1 个答案:

答案 0 :(得分:0)

请查看以下SELECT语句

;with cte as (
    select 0 as n union all 
    select 1 as n union all 
    select 2 as n
)
select
    ID,
    Flag,
    ValidFrom,
    case when r.ValidTo - r.ValidFrom > 2 then r.ValidFrom+2 else r.ValidTo end as ValidTo,
    ValidFrom + n as result
from ranges r, cte
where ValidFrom + n <= ValidTo