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

Convert Columns data into rows for the same ID

$
0
0

Hi All,

I have a table with 5 columns , let say  ID1,ID2,ExtID,ID3 and Types

My source data looks like this..

 ID1              ID2         ExtID       ID3       Types             
3255413    11988012      3        7836378     Home
3255413    11988013      4        7836379      Off
3255413    11988017      4        7836383      Off
3255413    11988021      3        7836389     Home
3255413    11988022      3        7865646     Home
3255413    11988023      4        7865647      Off
3264476    12024198      3        7871236     Home
3264476    12024199      4        7871237     Off
3265458    12028008      3        7835968     Home
3265458    12028008      4        7835968     Off

Rule 1- Always ExtID values will be In( 3, 4 )
Rule 2- Always Types values will be In ( Home, Off)
Rule 3- When ExtID=3 then Types will be 'Home'
        When ExtID=4 then Types will be 'Off'

I need to show Output like this..

NewID_1    NewID_2   HomeExt    NewID_3    HomeType   NewID1_2     NewID2_2    OffExt   NewID3_2    OffType
3255413     11988012            3            7836378       Home           3255413       11988013           4       7836379        Off
3255413     11988021            3            7836389       Home           3255413       11988017           4       7836383        Off
3255413     11988022            3            7865646       Home           3255413       1988023             4       7865647        Off
3264476     12024198            3            7871236       Home          3264476       12024199           4       7871237        Off
3265458     12028008            3            7835968       Home              Null               Null              Null         Null          Null
 Null              Null                 Null             Null             Null            3265458     12028008           4        7835968         Off

Create statement:

Create Table Test
(
ID1 Int null,
ID2 Int null,
ExtID Int null,
ID3 Int null,
Types varchar(10) null
)


Insert Statement:

Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988012,3,7836378,'Home')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988013,4,7836379,'Off')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988017,4,7836383,'Off')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988021,3,7836389,'Home')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988022,3,7865646,'Home')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3255413,11988023,4,7865647,'Off')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3264476,12024198,3,7871236,'Home')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3264476,12024199,4,7871237, 'Off')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3265458,12028008,3,7835968,'Home')
Insert into Test ( ID1,ID2,ExtID,ID3,Types) Values (3265458,12028008,4,7835968,'Off')


Thanks in Advance,
RH


sql


Viewing all articles
Browse latest Browse all 23857

Trending Articles



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