Using cursors to Nth records

  • Suppose you have a table with 100,000 records of various cities across the country and you wanted to select a certain amount per state. For example: NY:572, LA:485, FL:256, etc. Having another table with the state and quantities needed how can cursors be used to select the desired amount of records into a single table?

  • If the data table has an identity field or a unique field that is a clustered index order the order you want to use then this type of syntax may work.

    SELECT * FROM tblMain oq WHERE (SELECT COUNT(*) FROM tblMain iq WHERE iq.state = oq.state AND iq.[id] < oq.[id]) < (SELECT topamt FROM tblCtrl ic WHERE ic.state = oq.state)

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply