-- Get proper motions, JHK mag and errors, K-band extinction and radial
-- velocities, for stars with RVs > 300 km/s (with no BAD flags for RVs):
-- There is photometric data associated with each target, including proper
-- motions and other information. This example looks for such information
-- for larger (heliocentric) radial velocity stars. It restricts to objects
-- with good measured ASPCAP parameters. This requires joining the apogeeStar and
-- aspcapStar tables with the apogeeObject table, which has the target information.

SELECT TOP 100
   star.apogee_id, star.ra, star.dec, star.glon, star.glat, 
   star.vhelio_avg, star.vscatter,
   obj.j, obj.h, obj.k, obj.ak_targ, obj.ak_targ_method, obj.ak_wise,
   aspcap.teff, aspcap.logg, aspcap.metals
FROM apogeeStar star
   JOIN aspcapStar aspcap on aspcap.apstar_id = star.apstar_id
   JOIN apogeeObject obj on aspcap.target_id = obj.target_id
WHERE (aspcap.aspcapflag & dbo.fApogeeAspcapFlag('STAR_BAD')) = 0 and aspcap.teff > 0
    and (star.vhelio_avg > 300) and (star.starflag &
    dbo.fApogeeStarFlag('SUSPECT_RV_COMBINATION')) = 0
    and star.nvisits > 2 order by aspcap.apogee_id
