DataBase/SQL

소계 쿼리.

PCOSPD 100LRE SCRIE4A2 2021. 2. 3. 18:22

select 
  T_main.countryCode as `countryCode`, 
  T_main.entity as `entity`, 
  ifnull(T_main.saleType, 'Total') as `saleType`, 
  case
    when T_main.saleType is null then 'Total'
    when T_main.productName is null then 'sub-Total'
    else `T_main`.`productName`
  end as `productName`, 
  T_main.disCode as `disCode`, 
  case
    when T_main.saleType is null then '-'
    when T_main.productName is null then '-'
    else `T_main`.`itemName`
  end as `itemName`, 
  case
    when T_main.grossAmountSum = 0 then ''
    else T_main.grossAmountSum
  end as `sGrossAmountSum`, 
  case
    when T_main.grossAmountCnt = 0 then ''
    else T_main.grossAmountCnt
  end as `sGrossAmountCnt`, 
  case
    when T_main.adjustedAmountSum = 0 then ''
    else T_main.adjustedAmountSum
  end as `sAdjustedAmountSum`, 
  case
    when T_main.adjustedAmountCnt = 0 then ''
    else T_main.adjustedAmountCnt
  end as `sAdjustedAmountCnt`, 
  case
    when T_main.commissionAmountSum = 0 then ''
    else T_main.commissionAmountSum
  end as `sCommissionAmountSum`, 
  case
    when T_main.commissionAmountCnt = 0 then ''
    else T_main.commissionAmountCnt
  end as `sCommissionAmountCnt`
from (
  select 
    T_sub.COUNTRY_CODE as `countryCode`, 
    T_sub.ENTITY as `entity`, 
    T_sub.SALE_TYPE as `saleType`, 
    T_sub.PRODUCT_NAME as `productName`, 
    T_sub.disCode as `disCode`, 
    ITEM_MASTER.ITEM_NAME as `itemName`, 
    T_sub.grossAmountSum as `grossAmountSum`, 
    T_sub.grossAmountCnt as `grossAmountCnt`, 
    T_sub.adjustedAmountSum as `adjustedAmountSum`, 
    T_sub.adjustedAmountCnt as `adjustedAmountCnt`, 
    T_sub.commissionAmountSum as `commissionAmountSum`, 
    T_sub.commissionAmountCnt as `commissionAmountCnt`
  from (
    select 
      `completed_invoice`.`country_code`, 
      `completed_invoice`.`entity`, 
      `completed_invoice`.`sale_type`, 
      `completed_invoice`.`product_name`, 
      case
        when `completed_invoice`.`sale_type` is null then '-'
        when `completed_invoice`.`product_name` is null then '-'
        else `completed_invoice`.`dis_code`
      end as `disCode`, 
      sum(`completed_invoice`.`gross_amount`) as `grossAmountSum`, 
      count(case
        when `completed_invoice`.`gross_amount` = 0.0 then null
        else `completed_invoice`.`gross_amount`
      end) as `grossAmountCnt`, 
      sum(`completed_invoice`.`adjusted_amount`) as `adjustedAmountSum`, 
      count(case
        when `completed_invoice`.`adjusted_amount` = 0.0 then null
        else `completed_invoice`.`adjusted_amount`
      end) as `adjustedAmountCnt`, 
      sum(`completed_invoice`.`commission_amount`) as `commissionAmountSum`, 
      count(case
        when `completed_invoice`.`commission_amount` = 0.0 then null
        else `agp`.`completed_invoice`.`commission_amount`
      end) as `commissionAmountCnt`
    from `completed_invoice`
    where (
      `completed_invoice`.`country_code` = 'JP'
      and `completed_invoice`.`sales_month` = 201901
    )
    group by 
      `completed_invoice`.`entity`, 
      `completed_invoice`.`sale_type`, 
      `completed_invoice`.`product_name`, 
      `completed_invoice`.`dis_code`
    with rollup
  ) as `T_sub`
    left outer join (
      select 
        `ITEM_MASTER`.`display_code`, 
        `ITEM_MASTER`.`item_name`
      from `ITEM_MASTER`
      group by 
        `ITEM_MASTER`.`display_code`, 
        `ITEM_MASTER`.`item_name`
      
    ) as `ITEM_MASTER`
    on T_sub.disCode = ITEM_MASTER.DISPLAY_CODE
  where (
    T_sub.disCode is not null
    and T_sub.ENTITY is not null
  )
  order by 
    T_sub.ENTITY, 
    T_sub.SALE_TYPE, 
    T_sub.PRODUCT_NAME, 
    T_sub.disCode


) as `T_main`;