SQL分组日期按值更改

时间:2018-08-08 22:05:01

标签: sql hana

某些机构会帮助我使用SQL实现以下目标吗?在此先感谢社区-如果没有您,我们将一事无成。

输入数据

ITEM          YEAR WEEK QTY
SWEETWATER420   201828  1
SWEETWATER420   201829  1
SWEETWATER420   201830  1
SWEETWATER420   201831  1
SWEETWATER420   201832  1
SWEETWATER420   201833  150
SWEETWATER420   201834  150
SWEETWATER420   201835  150
SWEETWATER420   201836  150
SWEETWATER420   201837  150
SWEETWATER420   201838  1
SWEETWATER420   201839  1
SWEETWATER420   201840  1

所需结果

ITEM           QTY  BEGINNING YW    ENDING YW
SWEETWATER420   1   201828          201832
SWEETWATER420   150 201833          201837
SWEETWATER420   1   201838          201840

1 个答案:

答案 0 :(得分:1)

这是一个空白问题。 Hana支持row_number(),因此您可以使用行号的不同之处:

select item_qty, min(yearweek), max(yearweek)
from (select t.*,
             row_number() over (partition by item order by yearweek) as seqnum,
             row_number() over (partition by item, qty order by yearweek) as seqnum_1
      from t
     ) t
group by item, qty, (seqnum - seqnum_1)
order by item, qty, min(yearweek);