SELECT CASE WHEN GROUPING(gubun) = 0
THEN NVL(category,'소계')
ELSE '차감계'
END AS category
, CASE WHEN GROUPING(gubun) = 1
THEN SUM(DECODE(gubun,0,this_term,-this_term))
ELSE SUM(this_term)
END AS this_term
, CASE WHEN GROUPING(gubun) = 1
THEN SUM(DECODE(gubun,0,former_term,-former_term))
ELSE SUM(former_term)
END AS former_term
FROM (SELECT DECODE(t.catagory,'유동성장기차입금',1,0) AS gubun
, t.catagory
, SUM(DECODE(t.period_name,'08-Jun',t.amount_krw,0)) AS this_term
, SUM(DECODE(t.period_name,'08-Jun',0,t.amount_krw)) AS former_term
FROM klc_footnote_loan t
WHERE t.period_name IN ('08-Jun'
, TO_CHAR(ADD_MONTHS(TO_DATE('08-Jun', 'YY-Mon'), -12),'YY-Mon') )
GROUP BY DECODE(t.catagory,'유동성장기차입금',1,0), t.catagory
)
GROUP BY ROLLUP(gubun, category)
HAVING NOT (gubun = 1 AND GROUPING(category) = 1)