Please start any new threads on our new site at We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

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

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

Constraint Violating Yak Guru

274 Posts

Posted - 01/07/2014 :  20:22:54  Show Profile  Reply with Quote

I have this working pivot script from previous post

DECLARE @DateList varchar(5000), @SQL varchar(max), @batchid uniqueidentifier

set @batchid = '460f74fc-c318-4152-b758-4afa1db9c890'

SELECT @DateList = STUFF((SELECT DISTINCT ',[' + Dates + ']'
FROM View_2 where View_2.BatchID = @batchid
ORDER BY ',[' + Dates + ']'
FOR XML PATH('')),1,1,'')

FROM View_2 t
PIVOT (Max(t.empty) FOR t.Dates IN (' + @DateList + '))p
where p.batchid = '''+ cast(@batchid as varchar(100))+ ''''


The problem i have is i want to show 5 columns in each reports page.

For example, if i have 3 dates, naturally, it would have 3 columns in a pivot report but i want to display additional 2 empty date columns. In other word, i want to show groups of 5 date columns in a report.

So if i have dates in my result set that is not in groups of 5, i want add additional empty date columns to make it to 5.

How should i go about it.

many thanks in advance.

Very Important crosS Applying yaK Herder

52326 Posts

Posted - 01/08/2014 :  06:42:21  Show Profile  Reply with Quote
do you've a calendartable in your database? for your above requirement you need to start from calendar table to display 5 dates irespective of whether it has the data or not.
If you dont have one you can use function below to create it on the fly

SQL Server MVP
Go to Top of Page
  Previous Topic Topic Next 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.05 seconds. Powered By: Snitz Forums 2000