出现一组新数据时重置累积数字

Resetting Cumulative Figures when a new set of data appears

我有这个 table(减去 cuml 列):


¦  Name  ¦¦  website  ¦¦   page  ¦¦fruit type¦¦year week¦¦platform¦¦totalviews¦¦cuml¦
¦avocado ¦¦avocado.com¦¦aboutpage¦¦  sugar   ¦¦ 2001-08 ¦¦ mobile ¦¦     18   ¦¦ 18 ¦
¦avocado ¦¦avocado.com¦¦homepage ¦¦  sugar   ¦¦ 2001-08 ¦¦ desktop¦¦     10   ¦¦ 10 ¦
¦avocado ¦¦avocado.com¦¦homepage ¦¦  sugar   ¦¦ 2001-09 ¦¦ desktop¦¦     12   ¦¦ 22 ¦
¦avocado ¦¦avocado.com¦¦homepage ¦¦  sugar   ¦¦ 2001-10 ¦¦ desktop¦¦     6    ¦¦ 28 ¦
¦banana  ¦¦banana.com ¦¦aboutpage¦¦  fat     ¦¦ 2001-08 ¦¦tablet  ¦¦     21   ¦¦ 21 ¦
¦banana  ¦¦banana.com ¦¦contactus¦¦  fat     ¦¦ 2001-08 ¦¦tablet  ¦¦     14   ¦¦ 14 ¦
¦banana  ¦¦banana.com ¦¦homepage ¦¦  fat     ¦¦ 2001-08 ¦¦desktop ¦¦     15   ¦¦ 15 ¦
¦oranges ¦¦oranges.com¦¦aboutpage¦¦  sugar   ¦¦ 2001-09 ¦¦tablet  ¦¦     23   ¦¦ 23 ¦
¦oranges ¦¦oranges.com¦¦aboutpage¦¦  sugar   ¦¦ 2001-10 ¦¦tablet  ¦¦     15   ¦¦ 38 ¦
¦oranges ¦¦oranges.com¦¦contactus¦¦  sugar   ¦¦ 2001-08 ¦¦desktop ¦¦     6    ¦¦ 6  ¦

我想做的是 return 与 table 相同,但这次在末尾添加了 cuml 列。这个我试过了……


SELECT 

  [NAME]
, [WEBSITE]
, [PAGE]
, [FRUIT TYPE]
, [YEAR WEEK]
, [PLATFORM]
, [TOTALVIEWS]
, SUM(TOTALVIEWS) OVER(ORDER BY [REPORTING ISO YEAR WEEK] ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS CUML

FROM WEBVIEWSFORFRUITS

------if i place this WHERE clause in the statement the cuml column works...
--where [NAME] = 'AVACADO' and [PLATFORM] = 'DESKTOP'
------but i would like to this without the where clause...

ORDER BY [NAME], [WEBSITE], [PAGE], 
[FRUIT TYPE], [PLATFORM], [REPORTING ISO YEAR WEEK]

这很好,但是正如您从我的 table 中看到的那样,其他专栏提出了更大的挑战。我如何计算具有相同名称、网站、页面、水果类型、平台的每一行的累积频率,唯一的区别是年周的增量,当它点击一组不同的名称、网站、页面时,等等等等我需要将 cuml 列重置为该更改的总视图,并继续计算 cuml 直到它遇到一组新数据等并需要重置。所以在这个例子中,第 2 行 (avocado.com) 有 cuml。最多 28(第 4 行),然后在出现新的 data/weeks 时重置为 21 的总视图,并不断重置,直到它到达第 8 行和第 9 行,在那里它开始将前一行添加到 cuml (23+15 = 38) 中。 ..然后它重置为 6 作为它的新数据等。

我不完全确定我能做些什么来解决这个问题。

我考虑滞后函数?混合了某种触发语句?

我认为在 Over 子句中使用 Partition By 将创建所需的输出 -

declare @xyz table (
    Name varchar(50),
    website  varchar(50),
    page  varchar(50),
    fruittype varchar(50),
    yearweek varchar(50),
    platform varchar(50),
    totalviews int
)

insert into @xyz
select 'avocado' ,'avocado.com','aboutpage',  'sugar'   , '2001-08' ,'mobile'  ,18 union all
select 'avocado' ,'avocado.com','homepage' ,  'sugar'   , '2001-08' ,'desktop' ,10 union all
select 'avocado' ,'avocado.com','homepage' ,  'sugar'   , '2001-09' ,'desktop' ,12 union all
select 'avocado' ,'avocado.com','homepage' ,  'sugar'   , '2001-10' ,'desktop' ,6  union all
select 'banana'  ,'banana.com' ,'aboutpage',  'fat'     , '2001-08' ,'tablet'  ,21 union all
select 'banana'  ,'banana.com' ,'contactus',  'fat'     , '2001-08' ,'tablet'  ,14 union all
select 'banana'  ,'banana.com' ,'homepage' ,  'fat'     , '2001-08' ,'desktop' ,15 union all
select 'oranges' ,'oranges.com','aboutpage',  'sugar'   , '2001-09' ,'tablet'  ,23 union all
select 'oranges' ,'oranges.com','aboutpage',  'sugar'   , '2001-10' ,'tablet'  ,15 union all
select 'oranges' ,'oranges.com','contactus',  'sugar'   , '2001-08' ,'desktop' ,6

select *,
    sum(totalviews) over (partition by name, website, page, fruittype, platform order by yearweek rows between unbounded preceding and current row)
from @xyz