github.com/apache/beam/sdks/v2@v2.48.2/java/testing/tpcds/src/main/resources/queries/query51.sql (about)

     1  -- Licensed to the Apache Software Foundation (ASF) under one
     2  -- or more contributor license agreements.  See the NOTICE file
     3  -- distributed with this work for additional information
     4  -- regarding copyright ownership.  The ASF licenses this file
     5  -- to you under the Apache License, Version 2.0 (the
     6  -- "License"); you may not use this file except in compliance
     7  -- with the License.  You may obtain a copy of the License at
     8  --
     9  --     http://www.apache.org/licenses/LICENSE-2.0
    10  --
    11  -- Unless required by applicable law or agreed to in writing, software
    12  -- distributed under the License is distributed on an "AS IS" BASIS,
    13  -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  -- See the License for the specific language governing permissions and
    15  -- limitations under the License.
    16  
    17  WITH web_v1 as (
    18  select
    19    ws_item_sk item_sk, d_date,
    20    sum(sum(ws_sales_price))
    21        over (partition by ws_item_sk order by d_date rows between unbounded preceding and current row) cume_sales
    22  from web_sales
    23      ,date_dim
    24  where ws_sold_date_sk=d_date_sk
    25    and d_month_seq between 1212 and 1212+11
    26    and ws_item_sk is not NULL
    27  group by ws_item_sk, d_date),
    28  store_v1 as (
    29  select
    30    ss_item_sk item_sk, d_date,
    31    sum(sum(ss_sales_price))
    32        over (partition by ss_item_sk order by d_date rows between unbounded preceding and current row) cume_sales
    33  from store_sales
    34      ,date_dim
    35  where ss_sold_date_sk=d_date_sk
    36    and d_month_seq between 1212 and 1212+11
    37    and ss_item_sk is not NULL
    38  group by ss_item_sk, d_date)
    39   select  *
    40  from (select item_sk
    41       ,d_date
    42       ,web_sales
    43       ,store_sales
    44       ,max(web_sales)
    45           over (partition by item_sk order by d_date rows between unbounded preceding and current row) web_cumulative
    46       ,max(store_sales)
    47           over (partition by item_sk order by d_date rows between unbounded preceding and current row) store_cumulative
    48       from (select case when web.item_sk is not null then web.item_sk else store.item_sk end item_sk
    49                   ,case when web.d_date is not null then web.d_date else store.d_date end d_date
    50                   ,web.cume_sales web_sales
    51                   ,store.cume_sales store_sales
    52             from web_v1 web full outer join store_v1 store on (web.item_sk = store.item_sk
    53                                                            and web.d_date = store.d_date)
    54            )x )y
    55  where web_cumulative > store_cumulative
    56  order by item_sk
    57          ,d_date
    58  limit 100