Select records which have continuous values in a column

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mnmhema
    New Member
    • Oct 2013
    • 2

    Select records which have continuous values in a column

    I have a table with 3 columns. The first column has Hexadecimals. The second column has the corresponding binary numbers. The third colmn has boolean values.
    I need to select 4 records where the colC is False for the 4 records, the last two digits of the first record's colB (Binary value) are 00 and the other 3 records' ColB are the next consecutive binary numbers.
    After selecting the records, Col C of those records must be updated to True.
    I need these in a SQL function.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I have trouble following what you're trying to do. It would help to see sample data and results. Also, post what you've tried so far and any error messages that you get. What version of SQL Server are you using. Don't forget to use code tags when posting any code or formatted data.

    Comment

    • Luuk
      Recognized Expert Top Contributor
      • Mar 2012
      • 1043

      #3
      if i read this, i think he means:
      Code:
      select colA, colB, colC
      from tablename
      where colC=false 
        and colB>=(select colB 
                  from tablename
                  where right(colB,2)="00")
      limiting the output to 4 records in mssql is beyond my knowledge ;)

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        In MS SQL, you use SELECT TOP #.

        That query might error out. Your subquery can return more than one row.

        Comment

        • mnmhema
          New Member
          • Oct 2013
          • 2

          #5
          Originally posted by Rabbit
          I have trouble following what you're trying to do. It would help to see sample data and results. Also, post what you've tried so far and any error messages that you get. What version of SQL Server are you using. Don't forget to use code tags when posting any code or formatted data.
          Hello!
          I tried to simplify my question here. Hope you can understand what I mean.

          ColA has numbers like 450, 451, 452, ….
          ColB has Binary numbers like 1000,1001,1010, …..
          ColC has values like Free, Assigned, Assigend, Free….

          I need to select a group of 4 records where the first record’s binary number ends with 00, the Col C is Free and the ColB must have consecutive numbers like 455, 456,457, 458

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            That's an odd and specific requirement... Anyways, join the table to itself 3 times. One join for each consecutive row that you need.

            Comment

            Working...