--
-- 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.
--
-- q6 --
select
  a.ca_state state,
  count(*) cnt
from
  customer_address a,
  customer c,
  store_sales s,
  date_dim d,
  item i
where
  a.ca_address_sk = c.c_current_addr_sk
  and c.c_customer_sk = s.ss_customer_sk
  and s.ss_sold_date_sk = d.d_date_sk
  and s.ss_item_sk = i.i_item_sk
  and d.d_month_seq = (
    select
      distinct (d_month_seq)
    from
      date_dim
    where
      d_year = 2000
      and d_moy = 2
  )
  and i.i_current_price > 1.2 * (
    select
      avg(j.i_current_price)
    from
      item j
    where
      j.i_category = i.i_category
  )
group by
  a.ca_state
having
  count(*) >= 10
order by
  cnt,
  a.ca_state
limit
  100;
