Query write

DBA 21 Reputation points
2023-03-24T21:00:58.6466667+00:00

Am trying to update the same table by shuffling the column data, no condition to rearrange. I just want to make sure that the ID does not match with same car name again.

I tried my best with self join, Lead and LAG function still no luck.

any help greatly appreciated.

Create table #Temp

(

ID INT,

Carname Varchar(10)

)

Insert into #Temp Values (3,'AAAA')

Insert into #Temp Values (4,'BBBB')

Insert into #Temp Values (1,'CCCC')

Insert into #Temp Values (11,'DDDD')

Insert into #Temp Values (6,'EEEE')

Insert into #Temp Values (10,'FFFF')

Insert into #Temp Values (8,'GGGG')

Insert into #Temp Values (2,'HHHH')

Insert into #Temp Values (3,'IIII')

Insert into #Temp Values (7,'JJJJ')

Insert into #Temp Values (9,'KKKK')

Expecting results (shuffled data)

1 CCCC

2 HHHH

3 IIII

3 AAAA

4 BBBB

6 EEEE

7 JJJJ

8 GGGG

9 KKKK

10 FFFF

11 DDDD

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,671 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 112.1K Reputation points
    2023-03-25T08:45:58.0466667+00:00

    The shown output seems to correspond to select * from #Temp order by ID.

    Maybe you actually need this:

    select * from #Temp order by NEWID()
    
    -- or
    
    update t
    set ID = i
    from (select *, row_number() over (order by NEWID()) i from #Temp ) t
    
    0 comments No comments