github.com/apache/beam/sdks/v2@v2.48.2/java/testing/tpcds/src/main/resources/queries/query75.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 all_sales as (
    18   select d_year
    19         ,i_brand_id
    20         ,i_class_id
    21         ,i_category_id
    22         ,i_manufact_id
    23         ,sum(sales_cnt) as sales_cnt
    24         ,sum(sales_amt) as sales_amt
    25   from (select d_year
    26               ,i_brand_id
    27               ,i_class_id
    28               ,i_category_id
    29               ,i_manufact_id
    30               ,cs_quantity - coalesce(cr_return_quantity,0) as sales_cnt
    31               ,cs_ext_sales_price - coalesce(cr_return_amount,0.0) as sales_amt
    32         from catalog_sales join item on i_item_sk=cs_item_sk
    33                            join date_dim on d_date_sk=cs_sold_date_sk
    34                            left join catalog_returns on (cs_order_number=cr_order_number
    35                                                      and cs_item_sk=cr_item_sk)
    36         where i_category='Sports'
    37         union
    38         select d_year
    39               ,i_brand_id
    40               ,i_class_id
    41               ,i_category_id
    42               ,i_manufact_id
    43               ,ss_quantity - coalesce(sr_return_quantity,0) as sales_cnt
    44               ,ss_ext_sales_price - coalesce(sr_return_amt,0.0) as sales_amt
    45         from store_sales join item on i_item_sk=ss_item_sk
    46                          join date_dim on d_date_sk=ss_sold_date_sk
    47                          left join store_returns on (ss_ticket_number=sr_ticket_number
    48                                                  and ss_item_sk=sr_item_sk)
    49         where i_category='Sports'
    50         union
    51         select d_year
    52               ,i_brand_id
    53               ,i_class_id
    54               ,i_category_id
    55               ,i_manufact_id
    56               ,ws_quantity - coalesce(wr_return_quantity,0) as sales_cnt
    57               ,ws_ext_sales_price - coalesce(wr_return_amt,0.0) as sales_amt
    58         from web_sales join item on i_item_sk=ws_item_sk
    59                        join date_dim on d_date_sk=ws_sold_date_sk
    60                        left join web_returns on (ws_order_number=wr_order_number
    61                                              and ws_item_sk=wr_item_sk)
    62         where i_category='Sports') sales_detail
    63   group by d_year, i_brand_id, i_class_id, i_category_id, i_manufact_id)
    64   select  prev_yr.d_year as prev_year
    65                            ,curr_yr.d_year as`year`
    66                            ,curr_yr.i_brand_id
    67                            ,curr_yr.i_class_id
    68                            ,curr_yr.i_category_id
    69                            ,curr_yr.i_manufact_id
    70                            ,prev_yr.sales_cnt AS prev_yr_cnt
    71                            ,curr_yr.sales_cnt AS curr_yr_cnt
    72                            ,curr_yr.sales_cnt-prev_yr.sales_cnt AS sales_cnt_diff
    73                            ,curr_yr.sales_amt-prev_yr.sales_amt AS sales_amt_diff
    74   FROM all_sales curr_yr, all_sales prev_yr
    75   where curr_yr.i_brand_id=prev_yr.i_brand_id
    76     and curr_yr.i_class_id=prev_yr.i_class_id
    77     and curr_yr.i_category_id=prev_yr.i_category_id
    78     and curr_yr.i_manufact_id=prev_yr.i_manufact_id
    79     and curr_yr.d_year=2002
    80     and prev_yr.d_year=2002-1
    81     and cast(curr_yr.sales_cnt as decimal(17,2))/cast(prev_yr.sales_cnt as decimal(17,2))<0.9
    82   order by sales_cnt_diff
    83   limit 100