Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8202921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:21:52+00:00 2026-06-07T07:21:52+00:00

Is this possible in SQL? It seems like a simple concept. (I’m using SQL

  • 0

Is this possible in SQL? It seems like a simple concept. (I’m using SQL Server 2005)

Loop through all the rows in my SQL table
compare the values in column m if one row =65 and the next equals 120 increase j, and print that value in another column (column q)

Sub InsertProductionCycle() 

Dim LR As Long 
Dim j As Integer 

j = 1 
LR = Range("G" & Rows.Count).End(xlUp).Row 

    For i = 1 To LR Step 1 

        Cells(i, "Q").Value = j 

        If Cells(i, "M").Value = 65 And Cells(i + 1, "M").Value = 190 Then 
            j = j + 1 
        End If 
    Next i 

End Sub 

Any thoughts from the SQL experts? Can I do this in SQL?
My thoughts about this is:
Perhaps the loop is a bit of a stretch, (I can run that in a sql job), but can you compare the rows and insert the j loop value in another column. Thats where I’m stuck.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T07:21:54+00:00Added an answer on June 7, 2026 at 7:21 am

    This was a fun one! There may be a more efficient way to do this, but you can do it in a single set-based statement without the need for loops or cursors.

    Assuming a table that models your data that looks something like this (where i is basically your row number):

    CREATE TABLE MyTable (i int, M int)
    

    I used a self join to match i rows with i+1 rows and used a COUNT to figure out the Q column.

    ;WITH data AS (
        SELECT a.i, a.M, b.M as NextM
            , CASE WHEN a.M = 65 AND b.M = 190 THEN 1 ELSE 0 END AS shouldIncreaseQ
        FROM MyTable a
           LEFT OUTER JOIN MyTable b
              ON a.i + 1 = b.i
    )
    SELECT data.M, data.NextM
       , (SELECT COUNT(*) + 1 FROM data AS ref 
          WHERE ref.shouldIncreaseQ = 1 AND ref.i <= data.i) as Q
    FROM data
    

    If need be, you could use SELECT INTO to get the data into another table for future use.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is this possible? Using SQL Server 2005....... SELECT * FROM Data0304 UNION SELECT *
Is this possible to acces selector with a general tag name. Like in sql
Possible Duplicate: Concatenate row values T-SQL I have a table like this: ref_num name
This seems like the kind of thing Django makes simple, wondering if anyone has
I'm trying to do what seems like it should be a simple SQL operation,
Possible Duplicate: Get a list of dates between two dates This is my sql:
Is this possible to use Ajax.Beginform with update target inside of ajax form. like
Suppose a tree structure is implemented in SQL like this: CREATE TABLE nodes (
I've had a big problem in replicating a simple SQL Server 2008 R2 Express
We are using the SQL Server Database project template with Visual Studio 2010. As part of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.