
select
s.sourceID,
s.ra,s.dec,s.mergedClass,s.pStar,s.pGalaxy,s.pNoise,s.pSaturated,
s.eBV,s.aJ,s.aH,s.aK,
s.jclass,s.hclass,s.kclass,
s.PriOrSec,s.jppErrbits,s.hppErrbits,s.kppErrbits,1 as jrelflag
from udsSource as s
join udsMergeLog as ml on (s.framesetID=ml.framesetID)
 left outer join udsDetection as dj on (dj.multiframeID=ml.jmfID and dj.extnum=ml.jenum and dj.seqNum=s.jSeqNum)
 left outer join udsDetection as dh on (dh.multiframeID=ml.hmfID and dh.extnum=ml.henum and dh.seqNum=s.hSeqNum)
 left outer join udsDetection as dk on (dk.multiframeID=ml.kmfID and dk.extnum=ml.kenum and dk.seqNum=s.kSeqNum)
 join MultiframeDetector as mfdj on (mfdj.multiframeID=dj.multiframeID and mfdj.extnum=dj.extnum)
 join MultiframeDetector as mfdh on (mfdh.multiframeID=dh.multiframeID and mfdh.extnum=dh.extnum)
 join MultiframeDetector as mfdk on (mfdk.multiframeID=dk.multiframeID and mfdk.extnum=dk.extnum)
 join Multiframe as mfk on (mfk.multiframeid = mfdk.multiframeid)
 join Multiframe as mfh on (mfh.multiframeid = mfdh.multiframeid)
 join Multiframe as mfj on (mfj.multiframeid = mfdj.multiframeid)
where s.ra >= 32. and s.ra <= 38.5 and s.dec >= -8. and s.dec <= -1.
and (s.priorsec=0 or s.priorsec=s.framesetid) and (s.jppErrbits = 0)
union
select
s.sourceID,
s.ra,s.dec,s.mergedClass,s.pStar,s.pGalaxy,s.pNoise,s.pSaturated,
s.eBV,s.aJ,s.aH,s.aK,
s.jclass,s.hclass,s.kclass,
s.PriOrSec,s.jppErrbits,s.hppErrbits,s.kppErrbits,0 as jrelflag
from udsSource as s
join udsMergeLog as ml on (s.framesetID=ml.framesetID)
 left outer join udsDetection as dj on (dj.multiframeID=ml.jmfID and dj.extnum=ml.jenum and dj.seqNum=s.jSeqNum)
 left outer join udsDetection as dh on (dh.multiframeID=ml.hmfID and dh.extnum=ml.henum and dh.seqNum=s.hSeqNum)
 left outer join udsDetection as dk on (dk.multiframeID=ml.kmfID and dk.extnum=ml.kenum and dk.seqNum=s.kSeqNum)
 join MultiframeDetector as mfdj on (mfdj.multiframeID=dj.multiframeID and mfdj.extnum=dj.extnum)
 join MultiframeDetector as mfdh on (mfdh.multiframeID=dh.multiframeID and mfdh.extnum=dh.extnum)
 join MultiframeDetector as mfdk on (mfdk.multiframeID=dk.multiframeID and mfdk.extnum=dk.extnum)
 join Multiframe as mfk on (mfk.multiframeid = mfdk.multiframeid)
 join Multiframe as mfh on (mfh.multiframeid = mfdh.multiframeid)
 join Multiframe as mfj on (mfj.multiframeid = mfdj.multiframeid)
where s.ra >= 32. and s.ra <= 38.5 and s.dec >= -8. and s.dec <= -1.
and (s.priorsec=0 or s.priorsec=s.framesetid) and (s.jppErrbits != 0)
