
select
s.sourceID,
dh.AperMag1 as hAperMag1 ,dh.AperMag1Err as hAperMag1Err,
dh.AperMag2 as hAperMag2 ,dh.AperMag2Err as hAperMag2Err,
dh.AperMag3 as hAperMag3, dh.AperMag3Err as hAperMag3Err,
dh.AperMag4 as hAperMag4 ,dh.AperMag4Err as hAperMag4Err,
dh.AperMag5 as hAperMag5 ,dh.AperMag5Err as hAperMag5Err,
dh.AperMag6 as hAperMag6, dh.AperMag6Err as hAperMag6Err,
dh.AperMag7 as hAperMag7 ,dh.AperMag7Err as hAperMag7Err,
dh.AperMag8 as hAperMag8 ,dh.AperMag8Err as hAperMag8Err,
dh.AperMag9 as hAperMag9, dh.AperMag9Err as hAperMag9Err,
dh.AperMag10 as hAperMag10 ,dh.AperMag10Err as hAperMag10Err,
dh.AperMag11 as hAperMag11 ,dh.AperMag11Err as hAperMag11Err,
dh.AperMag12 as hAperMag12, dh.AperMag12Err as hAperMag12Err,
dh.AperMag13 as hAperMag13, dh.AperMag13Err as hAperMag13Err,
dh.isoMag as hisoMag,
dh.kronMag as hkronMag,dh.kronMagErr as hkronMagErr,
dh.kronRad as hkronRad,
dh.petroMag as hpetroMag, dh.petroMagErr as hpetroMagErr,
dh.petroRad as hpetroRad,
dh.psfMag as hpsfmag,dh.psfMagErr as hpsfmagerr,
dh.skyVar as hskyvar,dh.sky as hsky,
mfdh.aperCorPeak as hapercorpeak,mfdh.apercor1 as hapercor1,mfdh.apercor2 as hapercor2,mfdh.apercor3 as hapercor3,mfdh.apercor4 as hapercor4,mfdh.apercor5 as hapercor5,mfdh.apercor6 as hapercor6,mfdh.apercor7 as hapercor7,mfdh.photZPcat as hphotzpcat,mfdh.photzperrcat as hphotzperrcat,mfdh.skyNoise as hskynoise,mfdh.seeing as hseeing,mfdh.coreRadius as hcoreradius,mfdh.TotalExpTime as htotexptime,mfdh.extinctionCat as hextinctioncat,
mfh.amstart as hamstart,1 as hrelflag,
case mfh.amstart when 1 then
mfdh.photZPcat-(mfh.amstart-1)*mfdh.extinctionCat-2.5*log10(5*sqrt(3.14*mfdh.coreRadius*mfdh.coreRadius)*mfdh.skyNoise/mfh.exptime)-mfdh.apercor3 else 0.0 end as hmaglim
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 mfj on (mfj.multiframeid = mfdj.multiframeid)
 join Multiframe as mfh on (mfh.multiframeid = mfdh.multiframeid)
 join Multiframe as mfk on (mfk.multiframeid = mfdk.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.hppErrbits = 0)
union
select
s.sourceID,
dh.AperMag1 as hAperMag1 ,dh.AperMag1Err as hAperMag1Err,
dh.AperMag2 as hAperMag2 ,dh.AperMag2Err as hAperMag2Err,
dh.AperMag3 as hAperMag3, dh.AperMag3Err as hAperMag3Err,
dh.AperMag4 as hAperMag4 ,dh.AperMag4Err as hAperMag4Err,
dh.AperMag5 as hAperMag5 ,dh.AperMag5Err as hAperMag5Err,
dh.AperMag6 as hAperMag6, dh.AperMag6Err as hAperMag6Err,
dh.AperMag7 as hAperMag7 ,dh.AperMag7Err as hAperMag7Err,
dh.AperMag8 as hAperMag8 ,dh.AperMag8Err as hAperMag8Err,
dh.AperMag9 as hAperMag9, dh.AperMag9Err as hAperMag9Err,
dh.AperMag10 as hAperMag10 ,dh.AperMag10Err as hAperMag10Err,
dh.AperMag11 as hAperMag11 ,dh.AperMag11Err as hAperMag11Err,
dh.AperMag12 as hAperMag12, dh.AperMag12Err as hAperMag12Err,
dh.AperMag13 as hAperMag13, dh.AperMag13Err as hAperMag13Err,
dh.isoMag as hisoMag,
dh.kronMag as hkronMag,dh.kronMagErr as hkronMagErr,
dh.kronRad as hkronRad,
dh.petroMag as hpetroMag, dh.petroMagErr as hpetroMagErr,
dh.petroRad as hpetroRad,
dh.psfMag as hpsfmag,dh.psfMagErr as hpsfmagerr,
dh.skyVar as hskyvar,dh.sky as hsky,
mfdh.aperCorPeak as hapercorpeak,mfdh.apercor1 as hapercor1,mfdh.apercor2 as hapercor2,mfdh.apercor3 as hapercor3,mfdh.apercor4 as hapercor4,mfdh.apercor5 as hapercor5,mfdh.apercor6 as hapercor6,mfdh.apercor7 as hapercor7,mfdh.photZPcat as hphotzpcat,mfdh.photzperrcat as hphotzperrcat,mfdh.skyNoise as hskynoise,mfdh.seeing as hseeing,mfdh.coreRadius as hcoreradius,mfdh.TotalExpTime as htotexptime,mfdh.extinctionCat as hextinctioncat,
mfh.amstart as hamstart,0 as hrelflag,
case mfh.amstart when 1 then
mfdh.photZPcat-(mfh.amstart-1)*mfdh.extinctionCat-2.5*log10(5*sqrt(3.14*mfdh.coreRadius*mfdh.coreRadius)*mfdh.skyNoise/mfh.exptime)-mfdh.apercor3 else 0.0 end as hmaglim
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.hppErrbits != 0)
