将某个日期之前的收入汇总到 sql 中的该日期

Rolling up revenue before a date into that date in sql

我有一个 table "S8" 显示几本书的发行日期(这是发行周的星期六)、销量、收入和预计供应周数。这些值每周生成一次(星期六结束)

BOOKTITLE,BOOKDESCRIPTION,BOOKTYPE,RETAILER,WEEKSOLD,RELEASEDATEWEEK,UNITSSOLD,REVENUE,WEEKSOFSUPPLY
A,AAAAAAA,ORIGINAL,AMAZON,4/6/2019,4/20/2019,2,10,10
A,AAAAAAA,ORIGINAL,AMAZON,4/13/2019,4/20/2019,3,15,9
A,AAAAAAA,ORIGINAL,AMAZON,4/20/2019,4/20/2019,4,20,9
A,AAAAAAA,ORIGINAL,AMAZON,4/27/2019,4/20/2019,1,5,10
A,AAAAAAA,ORIGINAL,AMAZON,5/4/2019,4/20/2019,1,5,8
A,AAAAAAA,ORIGINAL,B&N,4/6/2019,4/20/2019,2,10,7
A,AAAAAAA,ORIGINAL,B&N,4/13/2019,4/20/2019,8,40,5
A,AAAAAAA,ORIGINAL,B&N,4/20/2019,4/20/2019,5,25,6
A,AAAAAAA,ORIGINAL,B&N,4/27/2019,4/20/2019,6,30,6
A,AAAAAAA,ORIGINAL,B&N,5/4/2019,4/20/2019,4,20,10
B,BBBBBBBB,SEQUEL,AMAZON,4/6/2019,4/13/2019,4,20,1
B,BBBBBBBB,SEQUEL,AMAZON,4/13/2019,4/13/2019,5,25,2
B,BBBBBBBB,SEQUEL,AMAZON,4/20/2019,4/13/2019,3,15,4
B,BBBBBBBB,SEQUEL,AMAZON,4/27/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,AMAZON,5/4/2019,4/13/2019,8,40,5
B,BBBBBBBB,SEQUEL,B&N,4/6/2019,4/13/2019,2,10,3
B,BBBBBBBB,SEQUEL,B&N,4/13/2019,4/13/2019,6,30,7
B,BBBBBBBB,SEQUEL,B&N,4/20/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,B&N,4/27/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,B&N,5/4/2019,4/13/2019,3,15,5

出于报告目的,我需要构建一个视图,将所有预售(发生在发行周前几周的图书购买)汇总到销售的第一周。

澄清一下,每本书都有一个预售期,您可以在该书实际发行之前购买该书。我需要生成一个视图,将发布日期之前发生的所有预售添加到销售的第一周。因此,如果这本书在发行周售出 4 件,在预售期间售出 10 件,我需要将第 1 周报告为 14 件,并从数据集中删除发行周之前的日期。

示例:

BOOKTITLE,BOOKDESCRIPTION,BOOKTYPE,RETAILER,WEEKSOLD,RELEASEDATEWEEK,UNITSSOLD,REVENUE,WEEKSOFSUPPLY
A,AAAAAAA,ORIGINAL,AMAZON,4/20/2019,4/20/2019,9,45,9
A,AAAAAAA,ORIGINAL,AMAZON,4/27/2019,4/20/2019,1,5,10
A,AAAAAAA,ORIGINAL,AMAZON,5/4/2019,4/20/2019,1,5,8
A,AAAAAAA,ORIGINAL,B&N,4/20/2019,4/20/2019,15,75,6
A,AAAAAAA,ORIGINAL,B&N,4/27/2019,4/20/2019,6,30,6
A,AAAAAAA,ORIGINAL,B&N,5/4/2019,4/20/2019,4,20,10
B,BBBBBBBB,SEQUEL,AMAZON,4/13/2019,4/13/2019,9,45,2
B,BBBBBBBB,SEQUEL,AMAZON,4/20/2019,4/13/2019,3,15,4
B,BBBBBBBB,SEQUEL,AMAZON,4/27/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,AMAZON,5/4/2019,4/13/2019,8,40,5
B,BBBBBBBB,SEQUEL,B&N,4/13/2019,4/13/2019,8,40,7
B,BBBBBBBB,SEQUEL,B&N,4/20/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,B&N,4/27/2019,4/13/2019,4,20,4
B,BBBBBBBB,SEQUEL,B&N,5/4/2019,4/13/2019,3,15,5

我可以使用此查询生成 Units/Revenue 汇总:

SELECT BOOKTITLE,BOOKDESCRIPTION,BOOKTYPE,RETAILER,RELEASEDATEWEEK, SUM(UNITSSOLD), SUM(REVENUE)
  FROM S8
  WHERE WEEKSOLD <= RELEASEDATEWEEK
  GROUP BY BOOKTITLE,BOOKDESCRIPTION,BOOKTYPE,RETAILER,RELEASEDATEWEEK

我不确定如何使用视图在适当的地方用汇总值替换值,也不知道如何只保留最近的供应周数(不需要汇总它们我只需要最近的发布日期周)

有条件的 "if the weeksold date is before the releasedateweek date then use the releasedateweek date else use the weeksold date" 怎么样?在 SQL 中看起来像 CASE WHEN WEEKSOLD < RELEASEDATEWEEK THEN RELEASEDATEWEEK ELSE WEEKSOLD END AS ROLLED_SOLD。然后你可以总结和分组,假设你在 S8 的每一行都有发布日期:

SELECT 
  BOOKTITLE, 
  BOOKDESCRIPTION,
  BOOKTYPE,
  RETAILER,
  CASE WHEN WEEKSOLD < RELEASEDATEWEEK THEN RELEASEDATEWEEK ELSE WEEKSOLD END AS ROLLED_SOLD, 
  SUM(UNITSSOLD), 
  SUM(REVENUE)
FROM S8
GROUP BY 
  BOOKTITLE, 
  BOOKDESCRIPTION,
  BOOKTYPE,
  RETAILER,
  CASE WHEN WEEKSOLD < RELEASEDATEWEEK THEN RELEASEDATEWEEK ELSE WEEKSOLD END AS ROLLED_SOLD

希望这些是日期而不是 "strings that look like dates"。如果它们是字符串那么

  1. 严惩自己;)
  2. 将它们转换为日期(更改列类型并将它们存储为日期(首选)或即时转换它们(呃))