github.com/apache/beam/sdks/v2@v2.48.2/java/testing/tpcds/src/main/resources/queries/query14a.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 cross_items as
    18   (select i_item_sk ss_item_sk
    19   from item,
    20      (select iss.i_brand_id brand_id, iss.i_class_id class_id, iss.i_category_id category_id
    21       from store_sales, item iss, date_dim d1
    22       where ss_item_sk = iss.i_item_sk
    23                      and ss_sold_date_sk = d1.d_date_sk
    24         and d1.d_year between 1999 AND 1999 + 2
    25     intersect
    26       select ics.i_brand_id, ics.i_class_id, ics.i_category_id
    27       from catalog_sales, item ics, date_dim d2
    28       where cs_item_sk = ics.i_item_sk
    29         and cs_sold_date_sk = d2.d_date_sk
    30         and d2.d_year between 1999 AND 1999 + 2
    31     intersect
    32       select iws.i_brand_id, iws.i_class_id, iws.i_category_id
    33       from web_sales, item iws, date_dim d3
    34       where ws_item_sk = iws.i_item_sk
    35         and ws_sold_date_sk = d3.d_date_sk
    36         and d3.d_year between 1999 AND 1999 + 2) x
    37   where i_brand_id = brand_id
    38     and i_class_id = class_id
    39     and i_category_id = category_id
    40  ),
    41   avg_sales as
    42   (select avg(quantity*list_price) average_sales
    43    from (
    44       select ss_quantity quantity, ss_list_price list_price
    45       from store_sales, date_dim
    46       where ss_sold_date_sk = d_date_sk
    47         and d_year between 1999 and 2001
    48     union all
    49       select cs_quantity quantity, cs_list_price list_price
    50       from catalog_sales, date_dim
    51       where cs_sold_date_sk = d_date_sk
    52         and d_year between 1999 and 1999 + 2
    53     union all
    54       select ws_quantity quantity, ws_list_price list_price
    55       from web_sales, date_dim
    56       where ws_sold_date_sk = d_date_sk
    57         and d_year between 1999 and 1999 + 2) x)
    58   select channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales)
    59   from(
    60       select 'store' channel, i_brand_id,i_class_id
    61               ,i_category_id,sum(ss_quantity*ss_list_price) sales
    62               , count(*) number_sales
    63       from store_sales, item, date_dim
    64       where ss_item_sk in (select ss_item_sk from cross_items)
    65         and ss_item_sk = i_item_sk
    66         and ss_sold_date_sk = d_date_sk
    67         and d_year = 1999+2
    68         and d_moy = 11
    69       group by i_brand_id,i_class_id,i_category_id
    70       having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)
    71     union all
    72       select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantity*cs_list_price) sales, count(*) number_sales
    73       from catalog_sales, item, date_dim
    74       where cs_item_sk in (select ss_item_sk from cross_items)
    75         and cs_item_sk = i_item_sk
    76         and cs_sold_date_sk = d_date_sk
    77         and d_year = 1999+2
    78         and d_moy = 11
    79       group by i_brand_id,i_class_id,i_category_id
    80       having sum(cs_quantity*cs_list_price) > (select average_sales from avg_sales)
    81     union all
    82       select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantity*ws_list_price) sales , count(*) number_sales
    83       from web_sales, item, date_dim
    84       where ws_item_sk in (select ss_item_sk from cross_items)
    85         and ws_item_sk = i_item_sk
    86         and ws_sold_date_sk = d_date_sk
    87         and d_year = 1999+2
    88         and d_moy = 11
    89       group by i_brand_id,i_class_id,i_category_id
    90       having sum(ws_quantity*ws_list_price) > (select average_sales from avg_sales)
    91   ) y
    92   group by rollup (channel, i_brand_id,i_class_id,i_category_id)
    93   order by channel,i_brand_id,i_class_id,i_category_id
    94   limit 100
    95