填写瀑布中缺少的日期

时间:2014-11-26 20:05:03

标签: sql logic google-bigquery

我有一个创建瀑布的查询:

SELECT CONCAT(SUBSTR(snapshot_date, -4, 4),'-',SUBSTR(snapshot_date, -10, 2),'-', SUBSTR(snapshot_date, -7, 2)) snapshot, 
item_name, 
type, 
translated_order_type,
item_description, 
source_plan,
week_start_date,
qty
FROM A
WHERE 
concat(SUBSTR(snapshot_date, -4, 4),'-',SUBSTR(snapshot_date, -10, 2),'-', SUBSTR(snapshot_date, -7, 2)) >= 
strftime_usec(date_add(TIME_USEC_TO_WEEK(date_add(now(),-84,'DAY'),1),-1,'DAY'),'%Y-%m-%d')

结果如下所示:(在excel中创建数据透视表后,并不是完整图片)

         week_start_date
+------------+--------+--------+---------+---------+---------+---------+----------+
| snapshot   | 9/1/14 | 9/8/14 | 9/15/14 | 9/22/14 | 9/29/14 | 10/6/14 | 10/13/14 |
+------------+--------+--------+---------+---------+---------+---------+----------+
| 8/31/14    |    380 |     97 |     104 |      84 |      84 |      76 |       81 |
| 9/7/14     |        |    378 |      91 |      89 |      96 |      90 |      108 |
| 9/14/14    |        |        |     203 |      31 |      27 |      30 |       41 |
| 9/21/14    |        |        |         |     405 |     111 |      75 |       77 |
| 9/28/14    |        |        |         |         |     436 |      73 |       93 |
| 10/5/14    |        |        |         |         |         |     433 |       93 |
| 10/14/14   |        |        |         |         |         |         |      439 |
+------------+--------+--------+---------+---------+---------+---------+----------+

行标签(8/31 / 14,19 / 7/14等)是快照日期,列标签是week_start_date。结果看起来是因为每个快照日期在此日期都有关于项目的数量和其他信息,并且每个快照日期将在不同周内具有数量,即week_start_date。

这里的空白是因为每个快照日期只有快照日期或超出快照日期的数量。

e.g. snapshot 8/31/14 will have week_start_date from 9/1/14 to 10/31/14,
     snapshot 9/7/14 will have week_start_date from 9/8/14 to 10/20/14,
     snapshot 9/14/13 will have week_start_date from 9/15/14 to 10/27/14

我需要在bigquery sql中执行此操作,以填充那些没有上一个week_start_date的快照的week_start_date。意味着所有快照日期都具有相同的week_start_date集。(min将是最早的快照,max将是最新的快照。)我不知道该怎么做。

谢谢!

数据样本:

+----------+-----------+---------------------+------+
| snapshot | item_name | sdm_week_start_date | qty  |
+----------+-----------+---------------------+------+
| 11/9/14  |  86001741 | 11/10/14            |  512 |
| 11/9/14  |  86001741 | 11/10/14            | 1492 |
| 11/9/14  |  86001741 | 11/10/14            |   53 |
| 11/16/14 |  86001741 | 11/17/14            |  704 |
| 11/16/14 |  86001741 | 11/17/14            |   49 |
| 11/16/14 |  86001741 | 11/17/14            | 1665 |
| 11/16/14 |  86001741 | 11/17/14            |  384 |
| 11/16/14 |  86001741 | 11/17/14            |   32 |
| 11/9/14  |  86001741 | 11/17/14            |  156 |
| 11/9/14  |  86001741 | 11/17/14            |    1 |
| 11/9/14  |  86001741 | 11/24/14            |  152 |
| 11/16/14 |  86001741 | 11/24/14            |  192 |
| 11/23/14 |  86001741 | 11/24/14            | 1683 |
| 11/16/14 |  86001741 | 11/24/14            |   24 |
| 11/23/14 |  86001741 | 11/24/14            |   49 |
| 11/16/14 |  86001741 | 11/24/14            |  152 |
| 11/23/14 |  86001741 | 11/24/14            | 1280 |
| 11/23/14 |  86001741 | 11/24/14            |   96 |
| 11/23/14 |  86001741 | 12/1/14             |  156 |
| 11/9/14  |  86001741 | 12/1/14             |  156 |
| 11/16/14 |  86001741 | 12/1/14             |  156 |
| 11/23/14 |  86001741 | 12/8/14             |  220 |
| 11/9/14  |  86001741 | 12/8/14             |  178 |
| 11/16/14 |  86001741 | 12/8/14             |  220 |
| 11/9/14  |  86001741 | 12/15/14            |  178 |
| 11/23/14 |  86001741 | 12/15/14            |  220 |
| 11/16/14 |  86001741 | 12/15/14            |  220 |
| 11/23/14 |  86001741 | 12/22/14            |  221 |
| 11/9/14  |  86001741 | 12/22/14            |  179 |
| 11/23/14 |  86001741 | 12/22/14            |   64 |
| 11/9/14  |  86001741 | 12/22/14            |  504 |
| 11/23/14 |  86001741 | 12/22/14            |  552 |
| 11/9/14  |  86001741 | 12/22/14            |  768 |
| 11/16/14 |  86001741 | 12/22/14            |  221 |
| 11/16/14 |  86001741 | 12/22/14            | 1024 |
| 11/16/14 |  86001741 | 12/22/14            |  552 |
| 11/9/14  |  86001741 | 12/22/14            |   64 |
| 11/16/14 |  86001741 | 12/22/14            |   64 |
| 11/23/14 |  86001741 | 12/22/14            | 1024 |
| 11/23/14 |  86001741 | 12/29/14            |  220 |
| 11/16/14 |  86001741 | 12/29/14            |  220 |
| 11/9/14  |  86001741 | 12/29/14            |  178 |
| 11/16/14 |  86001741 | 1/5/15              |  221 |
| 11/23/14 |  86001741 | 1/5/15              |  221 |
| 11/9/14  |  86001741 | 1/5/15              |  221 |
| 11/23/14 |  86001741 | 1/12/15             |  216 |
| 11/16/14 |  86001741 | 1/12/15             |  216 |
| 11/9/14  |  86001741 | 1/12/15             |  216 |
| 11/16/14 |  86001741 | 1/19/15             |  218 |
| 11/16/14 |  86001741 | 1/19/15             |    5 |
| 11/23/14 |  86001741 | 1/19/15             |   32 |
| 11/9/14  |  86001741 | 1/19/15             | 1280 |
| 11/23/14 |  86001741 | 1/19/15             |  408 |
| 11/9/14  |  86001741 | 1/19/15             |  480 |
| 11/9/14  |  86001741 | 1/19/15             |    5 |
| 11/9/14  |  86001741 | 1/19/15             |  218 |
| 11/16/14 |  86001741 | 1/19/15             |   32 |
| 11/23/14 |  86001741 | 1/19/15             | 1152 |
| 11/23/14 |  86001741 | 1/19/15             |   10 |
| 11/9/14  |  86001741 | 1/19/15             |   64 |
| 11/16/14 |  86001741 | 1/19/15             |  408 |
| 11/23/14 |  86001741 | 1/19/15             |  218 |
| 11/16/14 |  86001741 | 1/19/15             | 1152 |
| 11/9/14  |  86001741 | 1/26/15             |  216 |
| 11/23/14 |  86001741 | 1/26/15             |  216 |
| 11/16/14 |  86001741 | 1/26/15             |  216 |
| 11/9/14  |  86001741 | 2/2/15              |  112 |
| 11/16/14 |  86001741 | 2/2/15              |  112 |
| 11/23/14 |  86001741 | 2/2/15              |  143 |
| 11/23/14 |  86001741 | 2/9/15              |  156 |
| 11/16/14 |  86001741 | 2/9/15              |  124 |
| 11/9/14  |  86001741 | 2/9/15              |  124 |
| 11/9/14  |  86001741 | 2/16/15             |  256 |
| 11/23/14 |  86001741 | 2/16/15             |  896 |
| 11/9/14  |  86001741 | 2/16/15             |  120 |
| 11/16/14 |  86001741 | 2/16/15             |  127 |
| 11/23/14 |  86001741 | 2/16/15             |  158 |
| 11/16/14 |  86001741 | 2/16/15             |   12 |
| 11/23/14 |  86001741 | 2/16/15             |  216 |
| 11/9/14  |  86001741 | 2/16/15             |  127 |
| 11/23/14 |  86001741 | 2/16/15             |   12 |
| 11/16/14 |  86001741 | 2/16/15             |  216 |
| 11/16/14 |  86001741 | 2/16/15             |  896 |
| 11/9/14  |  86001741 | 2/16/15             |   12 |
| 11/9/14  |  86001741 | 2/23/15             |  124 |
| 11/16/14 |  86001741 | 2/23/15             |  124 |
| 11/23/14 |  86001741 | 2/23/15             |  156 |
| 11/16/14 |  86001741 | 3/2/15              |  252 |
| 11/23/14 |  86001741 | 3/2/15              |  242 |
| 11/9/14  |  86001741 | 3/2/15              |  252 |
| 11/9/14  |  86001741 | 3/9/15              |  250 |
| 11/16/14 |  86001741 | 3/9/15              |  250 |
| 11/23/14 |  86001741 | 3/9/15              |  240 |
| 11/9/14  |  86001741 | 3/16/15             |   12 |
| 11/16/14 |  86001741 | 3/16/15             |  257 |
| 11/23/14 |  86001741 | 3/16/15             |   12 |
| 11/9/14  |  86001741 | 3/16/15             |  257 |
| 11/23/14 |  86001741 | 3/16/15             |  247 |
| 11/16/14 |  86001741 | 3/16/15             |   12 |
| 11/16/14 |  86001741 | 3/23/15             |   32 |
| 11/23/14 |  86001741 | 3/23/15             |   32 |
| 11/9/14  |  86001741 | 3/23/15             |  255 |
| 11/16/14 |  86001741 | 3/23/15             |  216 |
| 11/16/14 |  86001741 | 3/23/15             |  768 |
| 11/16/14 |  86001741 | 3/23/15             |  255 |
| 11/23/14 |  86001741 | 3/23/15             |  245 |
| 11/23/14 |  86001741 | 3/23/15             |  768 |
| 11/9/14  |  86001741 | 3/23/15             |   32 |
| 11/23/14 |  86001741 | 3/23/15             |  216 |
| 11/9/14  |  86001741 | 3/23/15             |  168 |
| 11/9/14  |  86001741 | 3/23/15             |  384 |
| 11/9/14  |  86001741 | 3/30/15             |  257 |
| 11/23/14 |  86001741 | 3/30/15             |  247 |
| 11/16/14 |  86001741 | 3/30/15             |  257 |
| 11/9/14  |  86001741 | 4/6/15              |  314 |
| 11/23/14 |  86001741 | 4/6/15              |  302 |
| 11/16/14 |  86001741 | 4/6/15              |  314 |
| 11/16/14 |  86001741 | 4/13/15             |    5 |
| 11/16/14 |  86001741 | 4/13/15             |  304 |
| 11/23/14 |  86001741 | 4/13/15             |    5 |
| 11/23/14 |  86001741 | 4/13/15             |  291 |
| 11/9/14  |  86001741 | 4/13/15             |    5 |
| 11/9/14  |  86001741 | 4/13/15             |  304 |
| 11/9/14  |  86001741 | 4/20/15             |  144 |
| 11/23/14 |  86001741 | 4/20/15             |  192 |
| 11/23/14 |  86001741 | 4/20/15             |   32 |
| 11/16/14 |  86001741 | 4/20/15             |  192 |
| 11/23/14 |  86001741 | 4/20/15             |  448 |
| 11/16/14 |  86001741 | 4/20/15             |   32 |
| 11/16/14 |  86001741 | 4/20/15             |  302 |
| 11/9/14  |  86001741 | 4/20/15             |   32 |
| 11/9/14  |  86001741 | 4/20/15             |  302 |
| 11/23/14 |  86001741 | 4/20/15             |  290 |
| 11/9/14  |  86001741 | 4/20/15             |  320 |
| 11/16/14 |  86001741 | 4/20/15             |  448 |
| 11/16/14 |  86001741 | 4/27/15             |  304 |
| 11/9/14  |  86001741 | 4/27/15             |  304 |
| 11/23/14 |  86001741 | 4/27/15             |  291 |
| 11/16/14 |  86001741 | 5/4/15              |  156 |
| 11/9/14  |  86001741 | 5/4/15              |  156 |
| 11/23/14 |  86001741 | 5/4/15              |  150 |
| 11/23/14 |  86001741 | 5/11/15             |  142 |
| 11/9/14  |  86001741 | 5/11/15             |  149 |
| 11/16/14 |  86001741 | 5/11/15             |  149 |
| 11/16/14 |  86001741 | 5/18/15             |    5 |
| 11/23/14 |  86001741 | 5/18/15             |  140 |
| 11/23/14 |  86001741 | 5/18/15             |    5 |
| 11/9/14  |  86001741 | 5/18/15             |    5 |
| 11/9/14  |  86001741 | 5/18/15             |  146 |
| 11/16/14 |  86001741 | 5/18/15             |  146 |
| 11/16/14 |  86001741 | 5/25/15             |   48 |
| 11/9/14  |  86001741 | 5/25/15             |  149 |
| 11/23/14 |  86001741 | 5/25/15             |  142 |
| 11/16/14 |  86001741 | 5/25/15             |  128 |
| 11/23/14 |  86001741 | 5/25/15             |   48 |
| 11/16/14 |  86001741 | 5/25/15             |  149 |
| 11/9/14  |  86001741 | 5/25/15             |   48 |
| 11/9/14  |  86001741 | 5/25/15             |  128 |
| 11/23/14 |  86001741 | 5/25/15             |  128 |
| 11/9/14  |  86001741 | 6/1/15              |  197 |
| 11/23/14 |  86001741 | 6/1/15              |  197 |
| 11/16/14 |  86001741 | 6/1/15              |  197 |
| 11/9/14  |  86001741 | 6/8/15              |  198 |
| 11/23/14 |  86001741 | 6/8/15              |  198 |
| 11/16/14 |  86001741 | 6/8/15              |  198 |
| 11/16/14 |  86001741 | 6/15/15             |  198 |
| 11/16/14 |  86001741 | 6/15/15             |    5 |
| 11/23/14 |  86001741 | 6/15/15             |  198 |
| 11/9/14  |  86001741 | 6/15/15             |  198 |
| 11/9/14  |  86001741 | 6/15/15             |    5 |
| 11/23/14 |  86001741 | 6/15/15             |    5 |
| 11/23/14 |  86001741 | 6/22/15             |  198 |
| 11/9/14  |  86001741 | 6/22/15             |  198 |
| 11/16/14 |  86001741 | 6/22/15             |  198 |
| 11/23/14 |  86001741 | 6/29/15             |  198 |
| 11/9/14  |  86001741 | 6/29/15             |  198 |
| 11/16/14 |  86001741 | 6/29/15             |  198 |
| 11/23/14 |  86001741 | 7/6/15              |  247 |
| 11/16/14 |  86001741 | 7/6/15              |  247 |
| 11/9/14  |  86001741 | 7/6/15              |  247 |
| 11/16/14 |  86001741 | 7/13/15             |  245 |
| 11/23/14 |  86001741 | 7/13/15             |  245 |
| 11/9/14  |  86001741 | 7/13/15             |  245 |
| 11/16/14 |  86001741 | 7/20/15             |  246 |
| 11/9/14  |  86001741 | 7/20/15             |  246 |
| 11/23/14 |  86001741 | 7/20/15             |  246 |
| 11/16/14 |  86001741 | 7/27/15             |  245 |
| 11/9/14  |  86001741 | 7/27/15             |  245 |
| 11/23/14 |  86001741 | 7/27/15             |  245 |
| 11/23/14 |  86001741 | 8/3/15              |   99 |
| 11/16/14 |  86001741 | 8/3/15              |   99 |
| 11/9/14  |  86001741 | 8/3/15              |   99 |
| 11/23/14 |  86001741 | 8/10/15             |   99 |
| 11/16/14 |  86001741 | 8/10/15             |   99 |
| 11/9/14  |  86001741 | 8/10/15             |   99 |
| 11/16/14 |  86001741 | 8/17/15             |  109 |
| 11/9/14  |  86001741 | 8/17/15             |  109 |
| 11/23/14 |  86001741 | 8/17/15             |  109 |
| 11/23/14 |  86001741 | 8/24/15             |  109 |
| 11/16/14 |  86001741 | 8/24/15             |  109 |
| 11/9/14  |  86001741 | 8/24/15             |  109 |
| 11/9/14  |  86001741 | 8/31/15             |  109 |
| 11/23/14 |  86001741 | 8/31/15             |  109 |
| 11/16/14 |  86001741 | 8/31/15             |  109 |
| 11/23/14 |  86001741 | 9/7/15              |  281 |
| 11/9/14  |  86001741 | 9/7/15              |  281 |
| 11/16/14 |  86001741 | 9/7/15              |  281 |
| 11/9/14  |  86001741 | 9/14/15             |  280 |
| 11/23/14 |  86001741 | 9/14/15             |  280 |
| 11/16/14 |  86001741 | 9/14/15             |  280 |
| 11/16/14 |  86001741 | 9/21/15             |  281 |
| 11/9/14  |  86001741 | 9/21/15             |  281 |
| 11/23/14 |  86001741 | 9/21/15             |  281 |
| 11/23/14 |  86001741 | 9/28/15             |  280 |
| 11/16/14 |  86001741 | 9/28/15             |  280 |
| 11/9/14  |  86001741 | 9/28/15             |  280 |
| 11/16/14 |  86001741 | 10/5/15             |  281 |
| 11/9/14  |  86001741 | 10/5/15             |  281 |
| 11/23/14 |  86001741 | 10/5/15             |  281 |
| 11/16/14 |  86001741 | 10/12/15            |  273 |
| 11/9/14  |  86001741 | 10/12/15            |  273 |
| 11/23/14 |  86001741 | 10/12/15            |  273 |
| 11/16/14 |  86001741 | 10/19/15            |  274 |
| 11/9/14  |  86001741 | 10/19/15            |  274 |
| 11/23/14 |  86001741 | 10/19/15            |  274 |
| 11/23/14 |  86001741 | 10/26/15            |  273 |
| 11/9/14  |  86001741 | 10/26/15            |  273 |
| 11/16/14 |  86001741 | 10/26/15            |  273 |
| 11/23/14 |  86001741 | 11/2/15             |  113 |
| 11/9/14  |  86001741 | 11/2/15             |  113 |
| 11/16/14 |  86001741 | 11/2/15             |  113 |
| 11/23/14 |  86001741 | 11/9/15             |  112 |
| 11/23/14 |  86001741 | 11/16/15            |  109 |
| 11/23/14 |  86001741 | 11/23/15            |  108 |
| 11/23/14 |  86001741 | 11/30/15            |  109 |
| 11/16/14 |  86001741 | 12/14/15            |  438 |
| 11/9/14  |  86001741 | 12/14/15            |  438 |
+----------+-----------+---------------------+------+

0 个答案:

没有答案