各位前輩好,最近想在資料庫上查出不同年份的資料,所下的sql如下
select BAN_NO,CERTIFICATE_NO,TO_CHAR(EFFECTIVE_DATE, 'YYYY-MM-DD')
from AEO_CERTIFICATE
where EFFECTIVE_DATE >= TO_DATE('20090101','yyyyMMDD')
and EFFECTIVE_DATE <= TO_DATE('20091231','yyyyMMDD')
and CERTIFICATE_NO != 'null'
order by EFFECTIVE_DATE ;
其中依照不同的年份下去改變此片段,所以會有多個查詢結果
EFFECTIVE_DATE >= TO_DATE('20100101','yyyyMMDD')
and EFFECTIVE_DATE <= TO_DATE('20101231','yyyyMMDD')
但是這些不同的年份資料中的編號會有重複的編號,請問我有辦法在下sql時將兩次的查詢結果相互比對並去除掉嗎?
一言以蔽之
聯集 UNION
select BAN_NO,CERTIFICATE_NO,TO_CHAR(EFFECTIVE_DATE, 'YYYY-MM-DD')
from AEO_CERTIFICATE
where EFFECTIVE_DATE >= TO_DATE('20090101','yyyyMMDD')
and EFFECTIVE_DATE <= TO_DATE('20091231','yyyyMMDD')
and CERTIFICATE_NO != 'null'
UNION
select BAN_NO,CERTIFICATE_NO,TO_CHAR(EFFECTIVE_DATE, 'YYYY-MM-DD')
from AEO_CERTIFICATE
where EFFECTIVE_DATE >= TO_DATE('20100101','yyyyMMDD')
and EFFECTIVE_DATE <= TO_DATE('20101231','yyyyMMDD')
and CERTIFICATE_NO != 'null'
ORDER BY EFFECTIVE_DATE ;