blob: 8a24510e30cd4f69bf892ca3c8531d1e21639cd1 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
USE tpcds;
SELECT
ROUND(SUM(ss.ss_net_profit) / SUM(ss.ss_ext_sales_price), 2) AS gross_margin,
i.i_category,
i.i_class,
GROUPING(i.i_category) + GROUPING(i.i_class) AS lochierarchy,
RANK() OVER (
PARTITION BY
GROUPING(i.i_category) + GROUPING(i.i_class),
CASE WHEN GROUPING(i.i_class) = 0 THEN i.i_category END
ORDER BY
SUM(ss.ss_net_profit) / SUM(ss.ss_ext_sales_price) ASC
) AS rank_within_parent
FROM store_sales ss, date_dim d, item i, store s
WHERE d.d_year = 1999
AND d.d_date_sk = ss.ss_sold_date_sk
AND i.i_item_sk = ss.ss_item_sk
AND s.s_store_sk = ss.ss_store_sk
AND s.s_state IN ['TN','TN']
GROUP BY
ROLLUP(i.i_category, i.i_class)
ORDER BY
lochierarchy DESC,
CASE WHEN lochierarchy = 0 THEN i.i_category END,
rank_within_parent
/* TODO(dmitry). makes result stable. revisit. */
, i.i_category, i.i_class
LIMIT 100;