
select
s.sourceID,
dj.AperMag1 as jAperMag1 ,dj.AperMag1Err as jAperMag1Err,
dj.AperMag2 as jAperMag2 ,dj.AperMag2Err as jAperMag2Err,
dj.AperMag3 as jAperMag3, dj.AperMag3Err as jAperMag3Err,
dj.AperMag4 as jAperMag4 ,dj.AperMag4Err as jAperMag4Err,
dj.AperMag5 as jAperMag5 ,dj.AperMag5Err as jAperMag5Err,
dj.AperMag6 as jAperMag6, dj.AperMag6Err as jAperMag6Err,
dj.AperMag7 as jAperMag7 ,dj.AperMag7Err as jAperMag7Err,
dj.AperMag8 as jAperMag8 ,dj.AperMag8Err as jAperMag8Err,
dj.AperMag9 as jAperMag9, dj.AperMag9Err as jAperMag9Err,
dj.AperMag10 as jAperMag10 ,dj.AperMag10Err as jAperMag10Err,
dj.AperMag11 as jAperMag11 ,dj.AperMag11Err as jAperMag11Err,
dj.AperMag12 as jAperMag12, dj.AperMag12Err as jAperMag12Err,
dj.AperMag13 as jAperMag13, dj.AperMag13Err as jAperMag13Err,
dj.isoMag as jisoMag,
dj.kronMag as jkronMag,dj.kronMagErr as jkronMagErr,
dj.kronRad as jkronRad,
dj.petroMag as jpetroMag, dj.petroMagErr as jpetroMagErr,
dj.petroRad as jpetroRad,
dj.psfMag as jpsfmag,dj.psfMagErr as jpsfmagerr,
dj.skyVar as jskyvar,dj.sky as jsky,
mfdj.aperCorPeak as japercorpeak,mfdj.apercor1 as japercor1,mfdj.apercor2 as japercor2,mfdj.apercor3 as japercor3,mfdj.apercor4 as japercor4,mfdj.apercor5 as japercor5,mfdj.apercor6 as japercor6,mfdj.apercor7 as japercor7,mfdj.photZPcat as jphotzpcat,mfdj.photzperrcat as jphotzperrcat,mfdj.skyNoise as jskynoise,mfdj.seeing as jseeing,mfdj.coreRadius as jcoreradius,mfdj.TotalExpTime as jtotexptime,mfdj.extinctionCat as jextinctioncat,
mfj.amstart as jamstart,1 as jrelflag,
case mfj.amstart when 1 then
mfdj.photZPcat-(mfj.amstart-1)*mfdj.extinctionCat-2.5*log10(5*sqrt(3.14*mfdj.coreRadius*mfdj.coreRadius)*mfdj.skyNoise/mfj.exptime)-mfdj.apercor3 else 0.0 end as jmaglim
from dxsSource as s
join dxsMergeLog as ml on (s.framesetID=ml.framesetID)
 left outer join dxsDetection as dj on (dj.multiframeID=ml.jmfID and dj.extnum=ml.jenum and dj.seqNum=s.jSeqNum)
 left outer join dxsDetection as dh on (dh.multiframeID=ml.hmfID and dh.extnum=ml.henum and dh.seqNum=s.hSeqNum)
 left outer join dxsDetection 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,
dj.AperMag1 as jAperMag1 ,dj.AperMag1Err as jAperMag1Err,
dj.AperMag2 as jAperMag2 ,dj.AperMag2Err as jAperMag2Err,
dj.AperMag3 as jAperMag3, dj.AperMag3Err as jAperMag3Err,
dj.AperMag4 as jAperMag4 ,dj.AperMag4Err as jAperMag4Err,
dj.AperMag5 as jAperMag5 ,dj.AperMag5Err as jAperMag5Err,
dj.AperMag6 as jAperMag6, dj.AperMag6Err as jAperMag6Err,
dj.AperMag7 as jAperMag7 ,dj.AperMag7Err as jAperMag7Err,
dj.AperMag8 as jAperMag8 ,dj.AperMag8Err as jAperMag8Err,
dj.AperMag9 as jAperMag9, dj.AperMag9Err as jAperMag9Err,
dj.AperMag10 as jAperMag10 ,dj.AperMag10Err as jAperMag10Err,
dj.AperMag11 as jAperMag11 ,dj.AperMag11Err as jAperMag11Err,
dj.AperMag12 as jAperMag12, dj.AperMag12Err as jAperMag12Err,
dj.AperMag13 as jAperMag13, dj.AperMag13Err as jAperMag13Err,
dj.isoMag as jisoMag,
dj.kronMag as jkronMag,dj.kronMagErr as jkronMagErr,
dj.kronRad as jkronRad,
dj.petroMag as jpetroMag, dj.petroMagErr as jpetroMagErr,
dj.petroRad as jpetroRad,
dj.psfMag as jpsfmag,dj.psfMagErr as jpsfmagerr,
dj.skyVar as jskyvar,dj.sky as jsky,
mfdj.aperCorPeak as japercorpeak,mfdj.apercor1 as japercor1,mfdj.apercor2 as japercor2,mfdj.apercor3 as japercor3,mfdj.apercor4 as japercor4,mfdj.apercor5 as japercor5,mfdj.apercor6 as japercor6,mfdj.apercor7 as japercor7,mfdj.photZPcat as jphotzpcat,mfdj.photzperrcat as jphotzperrcat,mfdj.skyNoise as jskynoise,mfdj.seeing as jseeing,mfdj.coreRadius as jcoreradius,mfdj.TotalExpTime as jtotexptime,mfdj.extinctionCat as jextinctioncat,
mfj.amstart as jamstart,0 as jrelflag,
case mfj.amstart when 1 then
mfdj.photZPcat-(mfj.amstart-1)*mfdj.extinctionCat-2.5*log10(5*sqrt(3.14*mfdj.coreRadius*mfdj.coreRadius)*mfdj.skyNoise/mfj.exptime)-mfdj.apercor3 else 0.0 end as jmaglim
from dxsSource as s
join dxsMergeLog as ml on (s.framesetID=ml.framesetID)
 left outer join dxsDetection as dj on (dj.multiframeID=ml.jmfID and dj.extnum=ml.jenum and dj.seqNum=s.jSeqNum)
 left outer join dxsDetection as dh on (dh.multiframeID=ml.hmfID and dh.extnum=ml.henum and dh.seqNum=s.hSeqNum)
 left outer join dxsDetection 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)
