我有这张桌子(减去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作为其新数据等。
我不能完全确定我能做些什么来解决这个问题。
我正在考虑滞后函数?混合了某种触发器声明?
答案 0 :(得分:1)
我认为在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