SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Dynamic Pivot
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Villanuev
Constraint Violating Yak Guru

315 Posts

Posted - 08/14/2012 :  09:15:57  Show Profile  Reply with Quote
Hi Guys,

I have a requirements to have the returndate as my header.
I use the pivot but i dont know on how to make this as dynamics pivot.
I have to get the month to date data.

THank you in advance.

Here is my sample data.


Create table #SampleData
(Returndate datetime,
Itemid nvarchar(35),
Qty int)

Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-06','HTC1201',100)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-06','LGE3539',200)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-13','HTC1201',150)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-13','LGE3539',100)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-20','HTC1201',400)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-20','LGE3539',400)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-27','HTC1201',100)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-07-27','LGE3539',100)
Insert into #SampleData (Returndate, Itemid, Qty) values ('2012-08-03','HTC1201',1000)

 
Result:
Itemid----6----13-------20-----27
---------------------------------
HTC1201--100---150-----400----100
LGE3539--200---100-----400----100


My Script:
SELECT *
FROM (SELECT Itemid, DAY(ReturnDate) AS trandate, Qty
      FROM #SampleData) AS D
  PIVOT(SUM(qty) FOR trandate IN([6],[13],[20],[27])) AS P
Order by Itemid  

YuvarajKrishna
Starting Member

India
4 Posts

Posted - 08/14/2012 :  09:47:51  Show Profile  Reply with Quote
Hi Villanuev Try This its will work

DECLARE @Columns VARCHAR(max)
DECLARE @DynamicSql VARCHAR(max)


Select @Columns = +Coalesce(+@Columns +'],[','') + ReturnDate
From
(
SELECT DISTINCT CONVERT(VARCHAR,ReturnDate,101) ReturnDate
FROM #SampleData
)A
SET @DynamicSql ='SELECT *
FROM (SELECT Itemid,
convert(varchar,ReturnDate,101) AS trandate,
Qty
FROM #SampleData
) AS D
PIVOT(SUM(qty) FOR trandate IN(['+@Columns+'])) AS P
Order by Itemid'

EXEC (@DynamicSql)

Yuvaraj
Go to Top of Page

yosiasz
Flowing Fount of Yak Knowledge

USA
1610 Posts

Posted - 08/14/2012 :  12:05:39  Show Profile  Click to see yosiasz's MSN Messenger address  Reply with Quote
using following sproc by madhivanan

http://sqlblogcasts.com/blogs/madhivanan/archive/2008/08/27/dynamic-pivot-in-sql-server-2005.aspx

exec dynamic_pivot 'SELECT Itemid, Qty from SampleData WHERE DAY(ReturnDate) <> 3' , 'DAY(ReturnDate)', 'SUM(qty)'


<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion

Edited by - yosiasz on 08/14/2012 12:06:47
Go to Top of Page

joe8079
Posting Yak Master

USA
115 Posts

Posted - 08/14/2012 :  18:36:34  Show Profile  Reply with Quote
Just had a quick comment about this post and a question. Wouldnt it be easier to use a matrix to do this dynamic pivot? I'm not sure if you have access to reporting services, but it seems like this could be done in a matrix by using column groups. If you dont have access to reporting services, then this code works fine, but your life would be much easier.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
48012 Posts

Posted - 08/14/2012 :  18:42:09  Show Profile  Reply with Quote
quote:
Originally posted by joe8079

Just had a quick comment about this post and a question. Wouldnt it be easier to use a matrix to do this dynamic pivot? I'm not sure if you have access to reporting services, but it seems like this could be done in a matrix by using column groups. If you dont have access to reporting services, then this code works fine, but your life would be much easier.


yep..if you're using SSRS as front end matix container would do the trick for you. Otherwise its best dealt with inside T-SQL using a function like in the link posted

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

Go to Top of Page

Villanuev
Constraint Violating Yak Guru

315 Posts

Posted - 08/15/2012 :  01:31:46  Show Profile  Reply with Quote
Thank you guys for the reply.

Hi Visakh & Joe8079,

Yes, i have an access to reporting services. actually my objective for this script is to use in SSRS report.
I will make a dataset using this scripts that will create a report in SSRS.

btw, youre saying about the matrix that do a dynamic pivot, can you please give me some idea on this. thanks.
Go to Top of Page

joe8079
Posting Yak Master

USA
115 Posts

Posted - 08/15/2012 :  17:32:59  Show Profile  Reply with Quote
Hey, its actually really, really easy to use a matrix container for what your doing. Basically, you would take whatever field you wanted to pivot on and add it as a column group. It works just like the dynamic pivot in sql, but much easier to work with and you can add some more detail to the report as well. You have row groups, column groups and a field with what you want to have as an aggregate.

here is a pretty good link. It does everything your trying to do in reporting services.

http://technet.microsoft.com/en-us/library/ms157334%28v=sql.100%29.aspx
Go to Top of Page

Villanuev
Constraint Violating Yak Guru

315 Posts

Posted - 08/16/2012 :  05:42:23  Show Profile  Reply with Quote
Thank you very much @Joe8079. it working now. its easier compare to dynamics pivot. very helpful..
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.09 seconds. Powered By: Snitz Forums 2000