
select
s.sourceID,
dk.AperMag1 as kAperMag1 ,dk.AperMag1Err as kAperMag1Err,
dk.AperMag2 as kAperMag2 ,dk.AperMag2Err as kAperMag2Err,
dk.AperMag3 as kAperMag3, dk.AperMag3Err as kAperMag3Err,
dk.AperMag4 as kAperMag4 ,dk.AperMag4Err as kAperMag4Err,
dk.AperMag5 as kAperMag5 ,dk.AperMag5Err as kAperMag5Err,
dk.AperMag6 as kAperMag6, dk.AperMag6Err as kAperMag6Err,
dk.AperMag7 as kAperMag7 ,dk.AperMag7Err as kAperMag7Err,
dk.AperMag8 as kAperMag8 ,dk.AperMag8Err as kAperMag8Err,
dk.AperMag9 as kAperMag9, dk.AperMag9Err as kAperMag9Err,
dk.AperMag10 as kAperMag10 ,dk.AperMag10Err as kAperMag10Err,
dk.AperMag11 as kAperMag11 ,dk.AperMag11Err as kAperMag11Err,
dk.AperMag12 as kAperMag12, dk.AperMag12Err as kAperMag12Err,
dk.AperMag13 as kAperMag13, dk.AperMag13Err as kAperMag13Err,
dk.isoMag as kisoMag,
dk.kronMag as kkronMag,dk.kronMagErr as kkronMagErr,
dk.kronRad as kkronRad,
dk.petroMag as kpetroMag, dk.petroMagErr as kpetroMagErr,
dk.petroRad as kpetroRad,
dk.psfMag as kpsfmag,dk.psfMagErr as kpsfmagerr,
dk.skyVar as kskyvar,dk.sky as ksky,
mfdk.aperCorPeak as kapercorpeak,mfdk.apercor1 as kapercor1,mfdk.apercor2 as kapercor2,mfdk.apercor3 as kapercor3,mfdk.apercor4 as kapercor4,mfdk.apercor5 as kapercor5,mfdk.apercor6 as kapercor6,mfdk.apercor7 as kapercor7,mfdk.photZPcat as kphotzpcat,mfdk.photzperrcat as kphotzperrcat,mfdk.skyNoise as kskynoise,mfdk.seeing as kseeing,mfdk.coreRadius as kcoreradius,mfdk.TotalExpTime as ktotexptime,mfdk.extinctionCat as kextinctioncat,
mfk.amstart as kamstart,1 as krelflag,
case mfk.amstart when 1 then
mfdk.photZPcat-(mfk.amstart-1)*mfdk.extinctionCat-2.5*log10(5*sqrt(3.14*mfdk.coreRadius*mfdk.coreRadius)*mfdk.skyNoise/mfk.exptime)-mfdk.apercor3 else 0.0 end as kmaglim
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 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 >= 239. and s.ra <= 247. and s.dec >= 52. and s.dec <= 58.
and (s.priorsec=0 or s.priorsec=s.framesetid) and (s.kppErrbits = 0)
union
select
s.sourceID,
dk.AperMag1 as kAperMag1 ,dk.AperMag1Err as kAperMag1Err,
dk.AperMag2 as kAperMag2 ,dk.AperMag2Err as kAperMag2Err,
dk.AperMag3 as kAperMag3, dk.AperMag3Err as kAperMag3Err,
dk.AperMag4 as kAperMag4 ,dk.AperMag4Err as kAperMag4Err,
dk.AperMag5 as kAperMag5 ,dk.AperMag5Err as kAperMag5Err,
dk.AperMag6 as kAperMag6, dk.AperMag6Err as kAperMag6Err,
dk.AperMag7 as kAperMag7 ,dk.AperMag7Err as kAperMag7Err,
dk.AperMag8 as kAperMag8 ,dk.AperMag8Err as kAperMag8Err,
dk.AperMag9 as kAperMag9, dk.AperMag9Err as kAperMag9Err,
dk.AperMag10 as kAperMag10 ,dk.AperMag10Err as kAperMag10Err,
dk.AperMag11 as kAperMag11 ,dk.AperMag11Err as kAperMag11Err,
dk.AperMag12 as kAperMag12, dk.AperMag12Err as kAperMag12Err,
dk.AperMag13 as kAperMag13, dk.AperMag13Err as kAperMag13Err,
dk.isoMag as kisoMag,
dk.kronMag as kkronMag,dk.kronMagErr as kkronMagErr,
dk.kronRad as kkronRad,
dk.petroMag as kpetroMag, dk.petroMagErr as kpetroMagErr,
dk.petroRad as kpetroRad,
dk.psfMag as kpsfmag,dk.psfMagErr as kpsfmagerr,
dk.skyVar as kskyvar,dk.sky as ksky,
mfdk.aperCorPeak as kapercorpeak,mfdk.apercor1 as kapercor1,mfdk.apercor2 as kapercor2,mfdk.apercor3 as kapercor3,mfdk.apercor4 as kapercor4,mfdk.apercor5 as kapercor5,mfdk.apercor6 as kapercor6,mfdk.apercor7 as kapercor7,mfdk.photZPcat as kphotzpcat,mfdk.photzperrcat as kphotzperrcat,mfdk.skyNoise as kskynoise,mfdk.seeing as kseeing,mfdk.coreRadius as kcoreradius,mfdk.TotalExpTime as ktotexptime,mfdk.extinctionCat as kextinctioncat,
mfk.amstart as kamstart,0 as krelflag,
case mfk.amstart when 1 then
mfdk.photZPcat-(mfk.amstart-1)*mfdk.extinctionCat-2.5*log10(5*sqrt(3.14*mfdk.coreRadius*mfdk.coreRadius)*mfdk.skyNoise/mfk.exptime)-mfdk.apercor3 else 0.0 end as kmaglim
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 >= 239. and s.ra <= 247. and s.dec >= 52. and s.dec <= 58.
and (s.priorsec=0 or s.priorsec=s.framesetid) and (s.kppErrbits != 0)
