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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Dynamic columns

Author  Topic 

DeepakD
Starting Member

5 Posts

Posted - 2011-10-28 : 02:22:44
Hi ,

Can anybody help me to find a solution for following problem:

Input table

Col1 col2 col3 col4
101 a 400 1
102 b 500 1
103 c 600 1
104 d 700 2
105 e 800 2

I will pass col4 values like 1 or 1,2
The Output table should be like as follows

If I pass col4=1 then

Col1 col2 Col_4_1
101 a 400
102 b 500
103 c 600

If i Pass col4 in (1,2) then

Col1 col2 Col_4_1 Col_4_2
101 a 400 Null
102 b 500 Null
103 c 600 Null
104 d Null 700
105 e Null 800

I am thankful to you if anybody give me a suggestion.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-10-28 : 02:55:53
please read this http://www.sommarskog.se/dynamic_sql.html


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 04:55:01
why do you want to put them to different columns?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DeepakD
Starting Member

5 Posts

Posted - 2011-10-28 : 05:17:34
Hi Thank you for your reply,

As per the UI team needs I have to put them in different columns.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-10-28 : 05:19:00
quote:
Originally posted by DeepakD

Hi Thank you for your reply,

As per the UI team needs I have to put them in different columns.


see this then

http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DeepakD
Starting Member

5 Posts

Posted - 2011-10-28 : 08:16:19
Thank You khtan!!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2011-10-28 : 10:00:35
You let front end developers tell you what to do...there's really something wrong...tell them it's a presentation issue

Brett

8-)

Hint: Want your questions answered fast? Follow the direction in this link
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx


Want to help yourself?

http://msdn.microsoft.com/en-us/library/ms130214.aspx

http://weblogs.sqlteam.com/brettk/

http://brettkaiser.blogspot.com/


Go to Top of Page
   

- Advertisement -