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

Help in Complex query

$
0
0

Hi, I want to update the data of table #t1 and #t2 using the sample data. Help on this update.

code_t1 column in #T1 and unique in #t1 and sharing the data in #t2 table. I want to update the @code_desc with starting 4 char in code_t1_desc colum in #t1. 

code_t2 column in #T2 is unique. I also want to update the code_t1 in #T2 table with #t1.code_t1.

Important: Some #t2.code_t1 are twice. Update should be same as existing setup.

We'll be using max of code_t1 and code_t2.

Declare @code_desc As varchar(10)

Declare @code_desc ='0334'

Select MAX(code_t1)+1 from #T1 --1008

Select MAX(code_t2)+1 from #T2 --9

create table #T1 (code_t1 numeric, code_t1_desc char(20),code_id numeric)

insert into #T1 values (1002,'0333_BBBB_EET',123)

insert into #T1 values (1003,'0333_BBBB_EEC',123)

insert into #T1 values (1004,'0333_BBBB_EET',123)

insert into #T1 values (1005,'0333_BBBB_ERC',123)

insert into #T1 values (1006,'0333_BBBB_EEG',123)

insert into #T1 values (1007,'0333_BBBB_ERG',123)

create table #T2 (code_t2 numeric, code_t1 numeric, code_t2_desc char(20),code_amt numeric)

insert into #T2 values (1,1002,'EET',100)

insert into #T2 values (2,1003,'EEC',100)

insert into #T2 values (3,1004,'ET',100)

insert into #T2 values (4,1005,'ER',100)

insert into #T2 values (5,1006,'0333_BBBB_EEG',60)

insert into #T2 values (6,1006,'0333_BBBB_EEG',40)

insert into #T2 values (7,1007,'0333_BBBB_ERG',60)

insert into #T2 values (8,1007,'0333_BBBB_ERG',40)

--Desired result for #T1 table

1008   0334_BBBB_EET   123

1009   0334_BBBB_EEC  123

1010   0334_BBBB_EET   123

1011   0334_BBBB_ERC  123

1012   0334_BBBB_EEG  123

1013   0334_BBBB_ERG  123

--Desired result for #T2 table

9    1008   EET   100

10  1009  EEC   100

11  1010  ET     100

12   1005  ER   100

13   1006   0334_BBBB_EEG     60

14   1006   0334_BBBB_EEG      40

15   1007   0334_BBBB_ERG     60

16   1007   0334_BBBB_ERG    40

Thanks.




Viewing all articles
Browse latest Browse all 23857

Trending Articles



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