sybase iq 이긴하지만 혹시 아실것같아서 글 올립니다.
SELECT
'잔액' GUBUN,
convert(int,DBSBJYEAR||'1231') DATE1,
sum(DBSBJCGJACNT) CG_CNT,
sum(DBSBJCGJAKEM) 금액
FROM DMKSBJ
WHERE DBSBJMONTH = 12
and DBSBJYEAR between 2012
and 2016
GROUP BY DBSBJYEAR
union
select '잔액' GUBUN,
20161231 DATE1,
count(*) CG_CNT, // 건수
sum(FGCMSCGKEM-isnull(FGCHSHSKEM,0)) FGCMSCGJKM // 금액
from
(
select
*
from DBA.DGBCMS
where FGCMSHGDAT <= 20161231 and
(FGCMSHHDAT = 0 or FGCMSHHDAT > 20120101
) lst
left outer join
(
select
FGCHSFCJUM,
FGCHSBOCOD,
FGCHSFCGYY,
FGCHSFCGNO,
sum(FGCHSHSKEM) FGCHSHSKEM
from DBA.DGBCHS
where FGCHSHGDAT <= 20161231
group by FGCHSFCJUM,FGCHSBOCOD,FGCHSFCGYY,FGCHSFCGNO
) chs
on lst.FGCMSFCJUM = chs.FGCHSFCJUM and
lst.FGCMSBOCOD = chs.FGCHSBOCOD and
lst.FGCMSFCGYY = chs.FGCHSFCGYY and
lst.FGCMSFCGNO = chs.FGCHSFCGNO
이게 당해년도 쿼리인데 전년도 쿼리를 union all 하고 싶습니다. 어떻게 하면 되나요?
당해년 금액 - 전년 금액 빼기가 최종 목적 입니다.