Share via

How to write update statement to table statusvalues where chemical id have more than one chemical status ?

ahmed salah 3,216 Reputation points
2020-09-03T00:53:41.997+00:00

How to write update statement to table statusvalues where chemical id have more than one chemical status ?

I work on SQL server 2012 I face issue ; I can't update status on table statusvalues where chemical id have more than one status

as example
1241 must update status "chemical id have multiple status" because chemicalid have 2 status Rohs and china
1600 not update status because it have only one status as LifeCycle .

create table #chemical
(
chemicalId int,
PartId int,
chemicalStatus nvarchar(50)
)
insert into #chemical(chemicalId,PartId,chemicalStatus)
values 
(1241, 2250,'Rohs'),
(1241, 2700,'Rohs'),
(1241, 2900,'China'),
(1600, 2950,'Lifecycle'),
(1600, 3000,'Lifecycle')

create table #statusvalues
(
chemicalid int,
status nvarchar(50)
)
insert into #statusvalues(chemicalid)
 values
 (1241),
 (1600)

Expected result :

chemicalid status
1241 chemical id have multiple status
1600 NULL

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL

A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories


3 answers

Sort by: Most helpful
  1. Viorel 127K Reputation points
    2020-09-03T09:17:27.927+00:00

    Also check an approach that does not determine explicitly the unneeded counts:

    update d
    set [status] = case when s2 is null then s1 else 'chemical id have multiple status' end
    from (
       select v.[status], v.chemicalid, c1.chemicalStatus s1, c2.chemicalStatus s2
       from #statusvalues v
       inner join #chemical c1 on c1.chemicalId = v.chemicalid
       left  join #chemical c2 on c2.chemicalId = c1.chemicalId and c2.chemicalStatus <> c1.chemicalStatus
    ) d
    

    Was this answer helpful?

    0 comments No comments

  2. Jingyang Li 5,901 Reputation points Volunteer Moderator
    2020-09-03T03:27:19.443+00:00
    ;with mycte as 
     (
     select chemicalId,count(distinct chemicalStatus) cnt from #chemical
     group by chemicalId)
    
     Merge  #statusvalues tgt 
     using mycte src on tgt.chemicalId = src.chemicalId
     When matched  and src.cnt>1  then
     Update
     set tgt.status= 'The chemical id has multiple status.' ;  
    

    Was this answer helpful?

    0 comments No comments

  3. MelissaMa-msft 24,246 Reputation points Moderator
    2020-09-03T01:11:52.027+00:00

    Hi @ahmed salah ,

    Please refer below:

    ;with cte as (  
     select chemicalId,count(distinct chemicalStatus) count from #chemical  
     group by chemicalId)  
          
     update a   
     set a.status= case when count>1 then 'chemical id have multiple status' else NULL end  
     from #statusvalues a   
     left join cte b on a.chemicalid=b.chemicalId  
          
     select * from #statusvalues  
    

    Output:
    chemicalid status
    1241 chemical id have multiple status
    1600 NULL

    Best regards
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    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.

    Was this answer helpful?

    0 comments No comments

Your answer

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