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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Problem in pivoting data

Author  Topic 

de4ever@gmail.com
Starting Member

36 Posts

Posted - 2012-01-27 : 13:00:58
Hi All,
I have some tables like

ContentTable:
--------------

ContentId Name BookId
1 Content1 1
2 Content2 1



Variable Table:
-------------------
VariableId Name
1 Var1
2 Var2
3 Var3
4 Var4


Book Consists of multiple contents and variablevalue may be there for both book and contents or either of them.

I have book level variable(with contentId null) and content level variable for a particular Book say Book1 as below


variablevalue Table

ID VariableId ContentId BookIId Value

1 1 1 1 Content1Var1Value
2 2 1 1 Content1Var2Value
3 1 NULL 1 Book1Var1Value
4 2 2 1 Content2Var2Value
5 3 NULL 1 Book1Var3Value


As you can see
I have var1 associated with both content and book and Var2 only associated with content and var3 only associated with Book.
Now I need output data as


Contents Var1 Var2 Var3
-------- ---- ----- -----

Book1Var1Value {None} Book1Var3Value

Content1 Content1Var1Value Content1Var2Value {None}

Content2 {None} Content2Var2Value {None}

Basically if an associattion is ther for particular variable at Book Level,but missing in content level then we need to put {None} in those place
Again if an associattion is there for particular variable at Content level ,but missing in book level then we need to put {None} in those place for book.
First row with empty content signifies book level variable.
Now I need help in pivoting data in above mentioned format .Please help me out .

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-01-27 : 13:49:03
see this

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
   

- Advertisement -