Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
kapiltulsan
Starting Member
2 Posts |
Posted - 2015-02-09 : 08:41:32
|
Hi Friend,I have following 4 rows in a table with field name as IDID--AABBCCDDI require output asID NewColumn-- ----------AA AABB AA~BBCC AA~BB~CCDD AA~BB~CC~DDcan anyone suggest anything for getting an output as thisThanks in advance |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-02-09 : 08:46:03
|
What is the relationship between the rows? Is it just the ID in ascending order? |
|
|
kapiltulsan
Starting Member
2 Posts |
Posted - 2015-02-09 : 23:32:13
|
Hi,Actual scenario is as followingThere are 2 tables1. Table Name : TRNNUM Field Name : TRNID2. Table Name : TRNPRD Field Name : TRNID, PRDIDSample of tablesTable TRNNUMTRNID-----123425653458Table TRNPRDTRNID PRDID----- -----1234 AA 1234 BB1234 CC2565 CC2565 EE2565 FF2565 HH3458 AA3458 BBDesired output isTempTableTRNID PRDID PRDSEQ----- ----- ------1234 AA START1234 BB START:AA1234 CC START:AA:BB2565 CC START2565 EE START:CC2565 FF START:CC:EE2565 HH START:CC:EE:FF3458 AA START3458 BB START:AACan you please suggest something.Thanks in advance |
|
|
waterduck
Aged Yak Warrior
982 Posts |
Posted - 2015-02-10 : 03:12:06
|
[code]declare @TRNNUM table(TRNID int)insert into @TRNNUM select1234 union all select2565 union all select3458declare @TRNPRD table(TRNID int, PRDID varchar(2))insert into @TRNPRD select 1234,'AA' union all select1234,'BB' union all select1234,'CC' union all select2565,'CC' union all select2565,'EE' union all select2565,'FF' union all select2565,'HH' union all select3458,'AA' union all select3458,'BB';with cte as( select ROW_NUMBER() over (partition by TRNID order by PRDID) as rn, * from @TRNPRD)select a.TRNID, B.PRDID,'START' + ISNULL((select ':' + cast(PRDID as varchar(2)) from cte c where b.TRNID = c.TRNID and b.rn > c.rn for xml path('')), '')from @TRNNUM ajoin cte bon a.TRNID = b.TRNID[/code] |
|
|
|
|
|
|
|