Sqlserver Update same data from the same table

Joaquim Costa 21 Reputation points
2021-01-19T23:11:01.357+00:00

I have a table that has registration of several CAR (repeated and not repeated), so I intend to update a field (active) of all repeated registrations leaving 1 more recent line.
Exemple of table data:
|ID| CAR| ACTIVE|
|1 |AAA-25-35 |0|
|2 |LDB-25-35 |0|
|3 |LDB-00-35 |0|
|4 |LDB-25-35 |0|
|5 |LDB-00-35 |0|
|6 |LDC-10-10 |0|
|7 |LDC-10-10 |0|
|8 |LDB-00-35 |0|
I want my data to be like this:
|ID| CAR| ACTIVE|
|1 |AAA-25-35 |0|
|2 |LDB-25-35 |1|
|3 |LDB-00-35 |1|
|4 |LDB-25-35 |0|
|5 |LDB-00-35 |1|
|6 |LDC-10-10 |1|
|7 |LDC-10-10 |0|
|8 |LDB-00-35 |0|

I tried to make this code, but it is not working correctly

UPDATE teste 
SET 
ACTIVE = 1
FROM
 (
 SELECT  COUNT (CAR ),  CAR, ID  FROM TESTE
        GROUP BY CAR, ID 
   HAVING  COUNT (CAR )> 1
  ORDER BY   COUNT (CAR ) DESC,  
 ID DESC   
     )  AS AA
    WHERE TESTE.ID = AA.ID
SQL Server Other
0 comments No comments
{count} votes

Accepted answer
  1. CathyJi-MSFT 22,396 Reputation points Microsoft External Staff
    2021-01-20T05:56:49.617+00:00

    Hi @Joaquim Costa ,

    If the ACTIVE column means the row is useful. I think the value of ACTIVE column for not repeated data need also to be 1. So, please try below T-SQL.

    -----create table  
    create table dbo.test( id int, CAR varchar(30), ACTIVE int)  
      
    insert into dbo.test(id, CAR, ACTIVE)  
    values   
    (1, 'AAA-25-35', 0),  
    (2, 'LDB-25-35', 0),  
    (3, 'LDB-00-35', 0),  
    (4, 'LDB-25-35', 0),  
    (5, 'LDB-00-35', 0),  
    (6, 'LDC-10-10', 0),  
    (7, 'LDC-10-10', 0),  
    (8, 'LDB-00-35', 0)  
      
     select * from dbo.test  
      
     ----update table  
     WITH CTE AS  
     (  
         SELECT   
             row_number() over (partition by CAR order by id) as t  
             , CAR , ACTIVE  
         FROM dbo.test  
     )  
     update CTE  
     SET ACTIVE = 1  
     WHERE t=1   
      
     select * from dbo.test  
      
     -----drop table  
    drop table if exists dbo.test  
    

    58459-screenshot-2021-01-20-135327.jpg
    58435-screenshot-2021-01-20-135403.jpg
    Best regards,
    Cathy


    If the response is helpful, please click "Accept Answer" and upvote it, thank you.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Joaquim Costa 21 Reputation points
    2021-01-21T19:58:30.837+00:00

    I tested it and it didn't work 100% well, because of the car that is not repeated. I need that you do not change the line that is not repeated, simply the repeated ones, simply leaving a line.

    0 comments No comments

  2. Joaquim Costa 21 Reputation points
    2021-01-22T16:49:42.977+00:00

    the problem was solved by changing the code like this:

    WITH CTE AS
    (
    SELECT ROW_NUMBER() OVER(PARTITION BY CAR ORDER BY ID) AS t,
    CAR,
    ACTIVE
    FROM Test
    )

    UPDATE CTE
    SET ACTIVE = 1
    WHERE t=1
    AND EXISTS (SELECT 1 FROM CTE c WHERE c.CAR = CTE.CAR GROUP BY CAR HAVING COUNT(*) > 1)
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.