Select every 79th record

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ddtpmyra
    Contributor
    • Jun 2008
    • 333

    Select every 79th record

    I have 400K records on my table. Now I wanted you pull out random records that falls every 79th records from top to bottom. How can you do that on sql?

    Your help is much appreciated.
    thanks,
    DM
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    yes .. try this

    $sql = "select * from table_name order by RAND() limit 79 "

    Comment

    • ddtpmyra
      Contributor
      • Jun 2008
      • 333

      #3
      just a clarification i wanted to select the records on my table that has 437K row any records that falls on every 79th records. To sum up i should get aroun 5,500 number of records filtering it from top to bottom.

      im using ms sql server and i had error

      SQL
      select * FROM employee with(nolock) order by RAND() limit 79
      Error Msg
      Code:
      Msg 102, Level 15, State 1, Line 1
      Incorrect syntax near 'limit'.

      Comment

      • prabirchoudhury
        New Member
        • May 2009
        • 162

        #4
        try with out "with (NOLOCK)"

        and that gonna wrk

        select * FROM employee order by RAND() limit 79

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          Moved to SQL Server forum.

          Comment

          • ddtpmyra
            Contributor
            • Jun 2008
            • 333

            #6
            prabirchoudhury , it's still not working same error of
            Msg 102, Level 15, State 1, Line 1
            Incorrect syntax near 'limit'.

            Comment

            • ck9663
              Recognized Expert Specialist
              • Jun 2007
              • 2878

              #7
              Use ROW_NUMBER() function as inside a subquery and put a WHERE on the outer query where RownumberReturn ed mod 79 = 0

              That's a pseudo-code.

              Happy Coding!

              --- CK

              Comment

              • ddtpmyra
                Contributor
                • Jun 2008
                • 333

                #8
                Originally posted by ck9663
                Use ROW_NUMBER() function as inside a subquery and put a WHERE on the outer query where RownumberReturn ed mod 79 = 0

                That's a pseudo-code.

                Happy Coding!

                --- CK
                im having syntax error on below sql query
                Msg 207, Level 16, State 1, Line 4
                Invalid column name 'RowNumber'.
                Msg 207, Level 16, State 1, Line 4
                Invalid column name 'RowNumber'.
                Code:
                SELECT *,
                    ROW_NUMBER() OVER (ORDER BY RAND()) AS 'RowNumber'
                    FROM users with (nolock)
                    where RowNumber between  79 and 0

                Comment

                • ck9663
                  Recognized Expert Specialist
                  • Jun 2007
                  • 2878

                  #9
                  Use it as a subquery instead.

                  Happy Coding!!!

                  --- CK

                  Comment

                  Working...