Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numbering CROSS Apply rows

I'd like to number the rows created by using cross apply. Lets say I have a table like this

key|value
---+-----
 1 |  A
 2 |  B

and I run

select * from t CROSS apply String_Split('x,y', ',')

Id like to get that result:

key|value|value|number
---+-----+-----+--------
 1 |  A  | x   |   1  
 1 |  A  | y   |   2  
 2 |  B  | x   |   1  
 2 |  B  | y   |   2  

But I have no idead how to achive this; Also the "number" should be resitant to ordering. Any Hints? Suggestions?

Thanks!

like image 779
Jaster Avatar asked Sep 14 '25 08:09

Jaster


1 Answers

Just another option if 2016+, is to use the JSON KEY. It would be a small matter to +1 on the KEY if needed

Example

Declare @YourTable Table ([id] int,[value] varchar(50))  
Insert Into @YourTable Values 
 (1,'A')
,(2,'B')


Select A.*
      ,B.*
 From  @YourTable
 Cross Apply (
                SELECT [Key]
                      ,[value]
                FROM OPENJSON('["'+replace('x,y',',','","')+'"]')
             ) B

Returns

id  value   Key value
1   A       0   x
1   A       1   y
2   B       0   x
2   B       1   y

EDIT XML / 2014 version

Select A.*
      ,B.*
 From  @YourTable A
 Cross Apply (
                Select RetSeq = row_number() over (order by 1/0)
                      ,RetVal = ltrim(rtrim(B.i.value('(./text())[1]', 'varchar(max)')))
                From  (Select x = Cast('<x>' + replace('x,y',',','</x><x>')+'</x>' as xml)) as A 
                Cross Apply x.nodes('x') AS B(i)
             ) B
like image 91
John Cappelletti Avatar answered Sep 15 '25 21:09

John Cappelletti