代码之家  ›  专栏  ›  技术社区  ›  VS1SQL

显示新数据集时重置累积数字

  •  0
  • VS1SQL  · 技术社区  · 6 年前

    我有这个表(减去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行),然后在出现新数据/周时重置为totalviews 21,并一直重置,直到到达第8行和第9行,开始将前一行添加到cuml中(23+15=38)。。。然后重置为6,作为新数据等。

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

    我在想一个滞后函数?混合了某种触发器语句?

    1 回复  |  直到 6 年前
        1
  •  1
  •   DatabaseCoder    6 年前

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

    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