-- Detections within a given magnitude range
-- Find all the P2 detections within a magnitude range in a given area (cone search).

DECLARE @T TABLE
(objid bigint, detectID bigint, ra float, dec float, trueMag float)

INSERT @T
  SELECT d.objID, d.detectID, d.ra, d.dec, d.trueMag
  FROM P2PsfFits d JOIN fgetNearbyObjEq(221.30, 1.5, 10) n
	ON d.objID = n.objID

SELECT *
FROM @t
WHERE trueMag between 22.0 and 23.0
ORDER BY objid,detectid