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 2005 Forums
 Transact-SQL (2005)
 Pivot Query

Author  Topic 

Progress2007
Starting Member

17 Posts

Posted - 2009-03-09 : 11:31:32
Hello
I have a table in which there are 18 columns and there is a date Column. Now what i want that mu data should display in this format


Col1 Col2 Col3 Date1 Date2 Date3 Date 4 and so on

means date will be in a specified range and for date there will be a duration value. i want to do this by Stored procedure.

PLease help me how can i do this.

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2009-03-09 : 12:24:52

SELECT [ID] ,[q1] ,[q2] ,[q3] ,[q4],
MAX(CASE WHEN [Datecol]>getdate()-7 THEN [Datecol] ELSE NULL END) AS Date1,
MAX(CASE WHEN [Datecol]> getdate()-14 and [Datecol] <= getdate()-7 THEN [Datecol] ELSE NULL END) AS Date12
FROM TableName Group by
[ID] ,[q1] ,[q2] ,[q3] ,[q4]
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2009-03-10 : 00:14:26
see this link
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/dynamic-crosstab-with-multiple-pivot-columns.aspx

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 04:23:11
is the number of date values fixed?
Go to Top of Page

Progress2007
Starting Member

17 Posts

Posted - 2009-03-10 : 04:48:01
Hi vikash
my problem is that is the date selected is for one week then it should sisplay Monday to Sunday ...means 7 columns
but is some one selectes for two week date then is should display 14 columns

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 04:50:02
ok in that case what you need is to use method suggested in link before.
alternatively, you can get this very easily in sql reporting tool using matrix container
Go to Top of Page

Progress2007
Starting Member

17 Posts

Posted - 2009-03-10 : 05:03:20
will u pls tell me something abt sql reporting tool using matrix container
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-03-10 : 05:17:00
have you used SQL Reporting services before? see below
http://www.simple-talk.com/sql/sql-server-2005/advanced-matrix-reporting-techniques/

Go to Top of Page
   

- Advertisement -