How to group data and provide index

amit srivastava 21 Reputation points
2022-01-28T16:25:26.127+00:00

Hi Folks!!

I have data in a table as below

Id. Startdate. ColA. ColB.

  1. 1 Jan 2020. A. B.
  2. 2 Jan 2020. A. B.
  3. 3 Jan 2020. A. C.
  4. 4 Jan 202. A. C.
  5. 5 Jan 2020. A. B.
  6. 6 Jan 2020. A. B.
  7. 7 Jan 2020. A. C.

Now I want to introduce one more column such as Index and that will provide same index to all the grouped record until date is in sequence and data will be like as below

Id. Startdate. ColA. ColB. index

  1. 1 Jan 2020. A. B. 1
  2. 2 Jan 2020. A. B. 1
  3. 3 Jan 2020. A. C. 2
  4. 4 Jan 202. A. C. 2
  5. 5 Jan 2020. A. B. 3
  6. 6 Jan 2020. A. B. 3
  7. 7 Jan 2020. A. C. 4

Please suggest.

Thanks
Amit Srivastava

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,690 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tom Cooper 8,466 Reputation points
    2022-01-28T17:27:10.437+00:00
    ;With cteRn As
    (Select Id, Startdate, ColA, ColB,
      Row_Number() Over(Order By Startdate, Id) As rn
    From @Sample),
    cteIndex As
    (Select r.Id, r.Startdate, r.ColA, r.ColB, r.rn, 1 As [Index]
    From cteRn r
    Where r.rn = 1
    Union All
    Select r.Id, r.Startdate, r.ColA, r.ColB, r.rn, 
      Case When i.ColA = r.ColA And i.ColB = r.ColB Then i.[Index] Else i.[Index] + 1 End As [Index] 
    From cteIndex i
    Inner Join cteRn r On i.rn+1 = r.rn)
    Select Id, Startdate, ColA, ColB, [Index] 
    From cteIndex;
    

    Tom


  2. Viorel 112.1K Reputation points
    2022-01-28T17:40:59.553+00:00

    Check a suggestion:

    ;
    with Q as
    (
        select *,
            iif(lag(ColA) over (order by Startdate) <> ColA or lag(ColB) over (order by Startdate) <> ColB, 9, NULL) f
        from ATable
    )
    select Id, Startdate, ColA, ColB, count(f) over (order by Startdate) + 1 as [index]
    from Q
    order by Startdate
    

  3. amit srivastava 21 Reputation points
    2022-05-02T11:17:46.053+00:00

    Hi @Viorel

    I have done the above with some work around, as detailed below

    Table : dbo.distinct_id_number: Will have only distinct Employee Employee Number with Row ID against every Employee NUmber.

    Table : ATable : contain complete set of data.

    Table : dbo.TablePostIndexInclusion : Will hold the new data with Proper Index.

    DECLARE @csgo INT
    DECLARE @Max INT
    SET @Max = (select MAX(Row_Num) c from dbo.distinct_id_number)
    --select @Max
    SET @csgo =(select MIN(Row_Num) c from dbo.distinct_id_number)
    WHILE ( @csgo <= @Max )
    BEGIN
    ; with Q as
    (
    select *,
    iif(lag(ColA) over (order by Startdate) <> ColA or lag(ColB) over (order by Startdate) <> ColB, 9, NULL) f
    from ATable b (nolock) JOIN dbo.distinct_id_number a (nolock) on b.employee_number=a.employee_number
    Where a.Row_num=@csgo
    )
    insert into dbo.TablePostIndexInclusion
    select Id, Startdate, ColA, ColB, count(f) over (order by Startdate) + 1 as [index]
    from Q
    SET @csgo = @csgo + 1
    END

    Thanks
    Amit

    0 comments No comments