ソースコード
with item_sum as (
    select
        d.ITEM_CODE as CODE
      , i.ITEM_NAME as NAME
      , sum(d.UNITPRICE * d.SALES_QTY) as SAL_AMT
    from
               SALES     s
    inner join SALES_DTL d using(SALES_NO)
    inner join ITEM      i using(ITEM_CODE)
    where
        '2023-06-01' <= s.SALES_DATE and s.SALES_DATE <= '2023-06-30'
    group by
        d.ITEM_CODE
), total_sum as (
    select
        sum(SAL_AMT) as TOTAL_AMT
    from
        item_sum
), cum_sum as (
    select
        CODE
      , NAME
      , SAL_AMT
      , TOTAL_AMT
      , SUM(SAL_AMT) over(order by SAL_AMT desc) as CML_AMT
    from
        item_sum
      , total_sum
), proportion as (
    select
        CODE
      , NAME
      , SAL_AMT
      , CML_AMT
      , round((SAL_AMT * 100 * 1.00 / TOTAL_AMT * 1.00) , 1) as SAL_COMP
      , round((CML_AMT * 100 * 1.00 / TOTAL_AMT * 1.00) , 1) as TTL_COMP
    from
        cum_sum
)
select
    CODE
  , NAME
  , SAL_AMT
  , CML_AMT
  , SAL_COMP || '%' as SAL_COMP
  , TTL_COMP || '%' as TTL_COMP
  , case
        when TTL_COMP <= 40.0 then 'A'
        when TTL_COMP <= 80.0 then 'B'
        else                       'C'
    end as RANK
from
    proportion
order by
    3 desc
  , 1 desc
;
提出情報
提出日時2023/06/19 12:15:17
コンテスト第7回 SQLコンテスト
問題ABC分析
受験者kamaoda
状態 (詳細)AC
(Accepted: 正答)
メモリ使用量93 MB
メッセージ
テストケース(通過数/総数)
3/3
状態
メモリ使用量
データパターン1
AC
80 MB
データパターン2
AC
93 MB
データパターン3
AC
93 MB