Jump to content

SQL Help


pandemkodi

Recommended Posts

1 hour ago, JollyBoy said:

@pandemkodi

use this


DECLARE @col NVARCHAR(MAX),@sql NVARCHAR(MAX);
SELECT @col= COALESCE(@col, '') + ',''' + value +''' as ['+ code+']'
FROM tablename
   SELECT @sql = 'select ' +stuff(@col,1,1,'');
   exec(@sql)

Wow  Thanks bayya ..its working but all the codes coming as columns ..for example if code1 is present twice then code1 column coming 2 times but i need 

like below ..value should come as row if same code value is present 

 

 pk         code                       value 

           1       code1                       10

          2        code2                        100

           3       code 3                       1000 

           4        code1                            20

expected :

Code1   code 2   code 3 

10            100            1000

20              NULL       NULL

 

coming as 

code1  code2    code3   code1 

10             100      1000     20

 

Link to comment
Share on other sites

15 minutes ago, pandemkodi said:

Wow  Thanks bayya ..its working but all the codes coming as columns ..for example if code1 is present twice then code1 column coming 2 times but i need 

like below ..value should come as row if same code value is present 

 

 pk         code                       value 

           1       code1                       10

          2        code2                        100

           3       code 3                       1000 

           4        code1                            20

expected :

Code1   code 2   code 3 

10            100            1000

20              NULL       NULL

 

coming as 

code1  code2    code3   code1 

10             100      1000     20

 

I am not DB2 guy but use these 

https://www.ibm.com/developerworks/community/blogs/SQLTips4DB2LUW/entry/pivoting_tables56?lang=en

https://stackoverflow.com/questions/13579143/how-can-i-pivot-a-table-in-db2

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...