-- Get ASPCAP parameters and errors for all stars that were targeted as part
-- of the main APOGEE survey:
-- The stellar parameters are available for all stars that had ASPCAP run
-- on them. However, this includes some spectra known to be bad as well as
-- stars targeted as part of ancillary programs of various
-- sorts. Restricting to the good, main survey targets requires checking on
-- target and catalog flags, as in the examples below:

SELECT TOP 100
   s.apogee_id,s.ra, s.dec, s.glon, s.glat,
   s.vhelio_avg,s.vscatter,
   a.teff, a.teff_err, a.logg, a.logg_err, a.metals, a.metals_err,
   a.alphafe, a.alphafe_err,
   dbo.fApogeeAspcapFlagN(a.aspcapflag),
   dbo.fApogeeStarFlagN(s.starflag)
FROM apogeeStar s
   JOIN aspcapStar a on a.apstar_id = s.apstar_id
WHERE (a.aspcapflag & dbo.fApogeeAspcapFlag('STAR_BAD')) = 0 and
    s.commiss = 0 and
    (s.apogee_target1 &
    (dbo.fApogeeTarget1('APOGEE_SHORT')+
    dbo.fApogeeTarget1('APOGEE_INTERMEDIATE')+
    dbo.fApogeeTarget1('APOGEE_LONG'))) != 0
