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

  • SEARCH
  • Home
  • 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 8262509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:46:52+00:00 2026-06-08T03:46:52+00:00

My problem stems from this: VBA (Excel) vs SQL – Comparing values in two

  • 0

My problem stems from this:

VBA (Excel) vs SQL – Comparing values in two rows (loop)

I’ve modified the script a tad and it looks like this:

;WITH data AS 
( 
   SELECT a.i, a.M, b.M as NextM, 
   CASE WHEN a.M = 65 AND b.M = 120 THEN 1 ELSE 0 END AS shouldIncreaseQ   
   FROM MyTable a       
   LEFT OUTER JOIN MyTable b      
   ON a.i + 1 = b.i 
), test as
(
   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 
)

So now lets select * from test. I get something that looks like this.

M      NextM     Q
65     65        1
65     65        1
65     120       2
120    63        2
65     120       3
120    NULL      3

NOW.. all I want to is take the Q column (where Q pertains to the NextM) and put that in my original mytable.

So something like this:

i     M      Q
1     65     1
2     65     1
3     120    2
4     63     2
5     120    3
6     55     3

I hope that makes sense. I have been playing with select into, and even insert into all DAY (literally hours) with no luck. (using every type of join you can imagine)

Thank you in advance. (also can someone tell me how to make my tables look nicer.. do you use html to do it or what)?

UPDATE:

;WITH data AS 
 ( 
   SELECT a.i, a.M, b.M as NextM, 
   CASE WHEN a.M = 65 AND b.M = 120 THEN 1 ELSE 0 END AS shouldIncreaseQ   
   FROM MyTable a       
   LEFT OUTER JOIN MyTable b      
   ON a.i + 1 = b.i 
 ), 
test as
(
   SELECT data.i, data.M, data.NextM, 
   (SELECT COUNT(*) + 1 FROM data AS ref 
   WHERE ref.shouldIncreaseQ = 1 AND ref.i <= data.i) as Q FROM data 
),
final as
(
   select a.i, a.M as zw_step,  b.Q as prodid
   from test a
   left outer join test b
   on a.i - 1 = b.i
)

SELECT final.i, final.zw_step, final.prodid
FROM final

Now.. the select statement is how I like it. EXCEPT the darn thing will not work with a insert into command.. (you might have to fool around with final).
I’m now afraid of CTE, I will probably never use them again this.

Thanks for your help everyone.

  • 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-08T03:46:53+00:00Added an answer on June 8, 2026 at 3:46 am

    Okay, I’m not quite sure if I understand correctly what you are looking for. But you might want to replace

    SELECT final.i, final.zw_step, final.prodid
    FROM final;
    

    with

    UPDATE MT
      SET MT.Q = F.prodid
      FROM MyTable AS MT
      INNER JOIN final AS F ON MT.i = F.i AND MT.M = F.zw_step;
    

    This will replace the values in column Q in MyTable with the values in column prodid in final. Is this what you’re trying to do? (Based on “NOW.. all I want to is take the Q column (where Q pertains to the NextM) and put that in my original mytable.”)

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

Sidebar

Related Questions

I believe my problem stems from my use of weights and the RelativeLayout that
I have a code problem which stems from the fact that I am using
my problem stems from the use of wxApp as far as I can tell.
This stems from a recent couple of posts I've made on events and memory
My problem stems from Emacs inserting the coding system headers into source files containing
Preface: First time really using JavaScript + jQuery, so my problem likely stems from
This question stems from this question that I asked yesterday. I've followed Theo's advice
Basically my problem stems from a desire to have the textbox portion be white,
This is a very common problem when Excel Worksheet or Chart is embedded into
This problem seems very simple to me, but I've been unable to fix it,

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.