Jump to content

SQL Help


pandemkodi

Recommended Posts

idi naa source table DB2

 

        pk         code                       value 

           1       code1                       10

          2        code2                        100

           3       code 3                       1000 

           

target       

                  code1   code2 code3 

                    10         100       1000

 

naku ila vastondi case stmt tho 

             code1            code2    code 3

                10                null              null 

             null                   100         null 

           null                      null              1000

NUlls raakudadhu and all in single row lo ravali 

please help

Link to comment
Share on other sites

17 minutes ago, pandemkodi said:

idi naa source table DB2

 

        pk         code                       value 

           1       code1                       10

          2        code2                        100

           3       code 3                       1000 

           

target       

                  code1   code2 code3 

                    10         100       1000

 

naku ila vastondi case stmt tho 

             code1            code2    code 3

                10                null              null 

             null                   100         null 

           null                      null              1000

NUlls raakudadhu and all in single row lo ravali 

please help

nee code padeyyi ikkada.

 

andhulo bokkalu ento chebutaru

Link to comment
Share on other sites

this is what i am using 

 select (CASE WHEN  CODE ='CODE1' THEN VALUE END) AS CODE1,
         (CASE WHEN  CODE ='CODE2' THEN VALUE END) AS CODE2,
         (CASE WHEN  CODE ='CODE3' THEN VALUE END) AS CODE3 FROM TABLE1
 

Link to comment
Share on other sites

42 minutes ago, pandemkodi said:

can u explain more bayya..i cant even aggregate as well

You just have to put colums (or) expressions for those columns (like case stmt) inside coalesce function...

like

coalesce (expression1, expression2, column3, etc)

Link to comment
Share on other sites

Select Coalesce (CASE WHEN  CODE ='CODE1' THEN VALUE END) AS CODE1,
         Coalesce (CASE WHEN  CODE ='CODE2' THEN VALUE END )AS CODE2,
         Coalesce (CASE WHEN  CODE ='CODE3' THEN VALUE END) AS CODE3)

FROM TABLE1
 

Corrected...try

 

Link to comment
Share on other sites

54 minutes 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)

^^

Link to comment
Share on other sites

1 hour ago, pushparajukatre said:

anni case statements mundu max petti try cheyi vasthundi 

max or min chestey i will loose some values ..will not work for my case 

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...