-- Objects selected by cuts on three tables.
-- Find the parameters for all objects in a selected field with
-- desired PSF width and range of columns.
-- Now we are using three tables, but it is still a simple query.

-- NOTE: This query times out (takes too long) without the "TOP 1000".

SELECT TOP 1000
   g.run,
   g.rerun,
   g.camCol,
   f.field,
   p.objID,
   p.ra,
   p.dec,
   p.Rowc,
   p.Colc,
   p.u,
   p.modelMagErr_u ,
   p.g,
   p.modelMagErr_g,
   p.r,
   p.modelMagErr_r,
   p.petroMag_r - p.reddening_r,
   p.petroMagErr_r,
   p.i,
   p.modelMagErr_i,
   p.z,
   p.status & 0x00002000,
   f.psfWidth_r
into mydb.sampletest
FROM
   photoObj p, field f, segment g
WHERE
  f.fieldid = p.fieldid
  and  f.segmentid = g.segmentid
  and  f.psfWidth_r > 1.2
  and  p.colc > 400.0
