Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help in update generic query

$
0
0

HI, Some super_id is wrong or null in my #cred table and I want to update with the correct super_id.
The best way is to COUNT max 'super_id' on the basis of column-'b_id' and 'bid_code'.
Query condition should be based on 'b_id' and 'bid_code'. Please help for the logic of this update query.
I want also add the column of 'Correct super_id is:' in the result with the correct super_id.

Tip. 'b_id' column data must exist in  'bid_code' column. Can we use like command to avoid any hard code in the update and create generic script for update.


Example of new column of 'Correct super_id is:
The record of b_id-'SLEP' and bid_code-'SLEP_LEN' has super_id-'2' three times, one time is '1' and one time is 'null'.
So max super_id is 2 for these and this is correct super_id.

drop table #cred
create table #cred (unique_id numeric,b_id char(10), bid_code char(10), super_id numeric)
insert into #cred values (10012,'SLEP','SLEP_LEN',2)
insert into #cred values (10013,'SLEP','SLEP_LEN',1)
insert into #cred values (10014,'SLEP','SLEP_LEN',2)
insert into #cred values (10015,'SLEP','SLEP_LEN',2)
insert into #cred values (10016,'SLEP','SLEP_LEN',null)

insert into #cred values (10017,'GHEP','GHEP_RET',44)
insert into #cred values (10018,'GHEP','GHEP_RET',44)
insert into #cred values (10019,'GHEP','GHEP_RET',44)
insert into #cred values (10020,'GHEP','GHEP_RET',22)
insert into #cred values (10021,'GHEP','GHEP_RET',null)

insert into #cred values (10022,'SDEP','SDEP_Full',77)
insert into #cred values (10023,'SDEP','SDEP_Full',77)
insert into #cred values (10024,'SDEP','SDEP_Full',55)
insert into #cred values (10025,'SDEP','SDEP_Full',77)
insert into #cred values (10026,'SDEP','SDEP_Full',null)

Select * from #cred order by unique_id


--Desired Results
unique_id    b_id    bid_code    super_id   Correct super_id is:
10012    SLEP          SLEP_LEN      2
10013    SLEP          SLEP_LEN      1             2
10014    SLEP          SLEP_LEN      2
10015    SLEP          SLEP_LEN      2
10016    SLEP          SLEP_LEN      NULL       2

10017    GHEP          GHEP_RET      44       
10018    GHEP          GHEP_RET      44
10019    GHEP          GHEP_RET      44
10020    GHEP          GHEP_RET      22          44
10021    GHEP          GHEP_RET      NULL      44

10022    SDEP          SDEP_Full     77
10023    SDEP          SDEP_Full     77
10024    SDEP          SDEP_Full     55           77
10025    SDEP          SDEP_Full     77
10026    SDEP          SDEP_Full     NULL       77

 

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>