github.com/apache/beam/sdks/v2@v2.48.2/java/testing/tpcds/src/main/resources/queries/query44.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  select  asceding.rnk, i1.i_product_name best_performing, i2.i_product_name worst_performing
    18  from(select *
    19       from (select item_sk,rank() over (order by rank_col asc) rnk
    20             from (select ss_item_sk item_sk,avg(ss_net_profit) rank_col 
    21                   from store_sales ss1
    22                   where ss_store_sk = 2
    23                   group by ss_item_sk
    24                   having avg(ss_net_profit) > 0.9*(select avg(ss_net_profit) rank_col
    25                                                    from store_sales
    26                                                    where ss_store_sk = 2
    27                                                      and ss_hdemo_sk is null
    28                                                    group by ss_store_sk))V1)V11
    29       where rnk  < 11) asceding,
    30      (select *
    31       from (select item_sk,rank() over (order by rank_col desc) rnk
    32             from (select ss_item_sk item_sk,avg(ss_net_profit) rank_col
    33                   from store_sales ss1
    34                   where ss_store_sk = 2
    35                   group by ss_item_sk
    36                   having avg(ss_net_profit) > 0.9*(select avg(ss_net_profit) rank_col
    37                                                    from store_sales
    38                                                    where ss_store_sk = 2
    39                                                      and ss_hdemo_sk is null
    40                                                    group by ss_store_sk))V2)V21
    41       where rnk  < 11) descending,
    42  item i1,
    43  item i2
    44  where asceding.rnk = descending.rnk 
    45    and i1.i_item_sk=asceding.item_sk
    46    and i2.i_item_sk=descending.item_sk
    47  order by asceding.rnk
    48  limit 100