-- This query selects multiply-detected sources (stars) in photometry, 
-- using the thingIndex and detectionIndex tables. thingIndex contains 
-- a list of all unique sources, with a primary key defined called thingId, 
-- and detectionIndex contains the objId of each observation of each 
-- thingId. The objId can be used to join with the Star view of the photoObjAll table 
-- in order to recover the photometric data associated with each stellar observation. 

SELECT TOP 10 t.thingid, t.ndetect, d.objid, p.psfMag_r,
p.psfMagerr_r 
FROM thingIndex AS t
JOIN detectionindex AS d ON t.thingid = d.thingid
JOIN Star AS p ON d.objid = p.objid 
WHERE t.ndetect > 1
