-- Get SDSS ugriz photometry, errors and flags, ASPCAP parameters for the APOGEE stars with b > 60:
-- In addition to matching to the SDSS spectroscopy, you can also match to
-- the SDSS photometric imaging data. In this case, we only give an example
-- within CAS. To do this purely with flat files requires either
-- downloading the full photometric catalog (about 3 Tbytes) or the
-- "datasweep" files (about 300 Gbytes), both described in the imaging data
-- access documentation, and constructing an efficient flat-file method to
-- do the matching. For most purposes, CAS will be the right way to do this.

SELECT TOP 50
    photoobj.run, photoobj.camcol, photoobj.field, photoobj.obj,
    photoobj.psfmag_u, photoobj.psfmag_g, photoobj.psfmag_r,
    photoobj.psfmag_i, photoobj.psfmag_z, 
    photoobj.ra as photoobj_ra, photoobj.dec as photoobj_dec, 
    star.apstar_id, star.ra as star_ra, star.dec as star_dec, 
    aspcap.teff, aspcap.metals, aspcap.logg
FROM apogeeStar AS star 
    CROSS APPLY dbo.fGetNearestObjEq( star.ra, star.dec, 0.05) AS near 
    JOIN photoobj ON near.objid=photoobj.objid 
    JOIN aspcapStar as aspcap ON star.apstar_id = aspcap.apstar_id
WHERE star.glat > 60. and aspcap.teff > 0
