【问题标题】:Resetting Cumulative Figures when a new set of data appears出现一组新数据时重置累积数字
【发布时间】:2018-08-22 18:59:59
【问题描述】:

我有这张表(减去 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  ¦

我想要做的是返回同一张表,但这次最后有 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]

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

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

我正在考虑延迟功能?混合了某种触发语句?

【问题讨论】:

  • 您在Over 子句中是否缺少Partition by
  • 发布预期结果会很有帮助,您的 consumable 格式的样本数据将非常宝贵。
  • @larnu 预期结果如上表

标签: sql sql-server triggers lag cumulative-frequency


【解决方案1】:

我认为在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    • 2020-11-28
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多