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`;
'DataBase > SQL' 카테고리의 다른 글
[SQL/쿼리 튜닝] 조인조건 변경을 통한 속도 개선 (0) | 2021.05.13 |
---|---|
한 컬럼에 대해서 두 개 이상의 조건을 적용하는 쿼리 (0) | 2021.05.13 |
[MySQL] Pivot 쿼리. (0) | 2021.02.03 |
[SQL/TABLE/COLUMN] 테이블의 성격에 따라 다른 컬럼값 (0) | 2021.01.15 |
[MySQL/SQL] update 시 테이블 조인. update table join. (0) | 2021.01.12 |