79337552

Date: 2025-01-07 22:56:38
Score: 2.5
Natty:
Report link

Unfortunately you did not show, what were your attempts with CTE.

With cascading/nesting some CTEs it could be solved:

What about this:

WITH groupLimits AS ( -- all places where neighboring rows change (col2 in (1,3) vs. col2 not in (1,3))
    SELECT A.Record_Number arn, A.Col1 ac1, A.Col2 ac2,
           B.Record_Number brn, B.Col1 bc1, B.Col2 bc2,
           CASE WHEN A.Col2 IN (1, 3) THEN 1 ELSE 0 END ain,
           CASE WHEN B.Col2 IN (1, 3) THEN 1 ELSE 0 END bin
    FROM tbl1 A
    JOIN tbl2 B
      ON A.Record_Number + 1 = B.Record_Number
         AND (
               (A.Col2 IN (1, 3) AND B.Col2 NOT IN (1, 3))
               OR
               (A.Col2 NOT IN (1, 3) AND B.Col2 IN (1, 3))
             )
    order by A.Record_Number
), groups as (
   select ...
   from groupLimits C
   join groupLimits D
     on ...
...
select ...
...

With above groupLimits you get the idea where groups of (neighbor) rows, that have the same condition, either Col2 IN (1, 3) or Col2 NOT IN (1, 3) end (even if the group contains only 1 row).

The (neighboring) entries of groupLimits could be joined again with each other to get the minimum and maximum record_number of a group of tbl1 entries, that have all either Col2 IN (1, 3) or Col2 NOT IN (1, 3) (ain, bin can help). Perhaps you have to add an additional entry for first and last of record_number in tbl1.

Then you can calculate the col3 value for a group of rows with Col2 NOT IN (1, 3) (e. g. record_number 3 and 4), that is the value of record_number 2 (before 3) = 456. The col3 value for all records in a group with rows with Col2 IN (1, 3) is anyway its col1 value and easy to handle.

Are these enough ideas/hints to solve it?

Reasons:
  • RegEx Blacklisted phrase (1.5): solve it?
  • Long answer (-1):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (0.5):
Posted by: BitLauncher