-- Get parameters for all stars with [Fe/H] < -2 with no BAD FLAGS set:
-- You can also select a subset of the stars based on their
-- properties. This example finds a set of metal-poor stars, without any
-- flags set indicating that the observations or analysis is bad.

SELECT TOP 100
   s.apogee_id,s.ra, s.dec, s.glon, s.glat,
   s.vhelio_avg,s.vscatter,
   a.teff,a.logg, a.metals, a.alphafe,
   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 a.teff > 0 and a.metals < -2
