Pivoted Table in sql server 2008

Dear all,

 I have this table :
 CASES:
 CaseID (int)
 CaseDate (date)
 CaseTypeID (int)

 using pivoted table I need to generate following result in specific date range:
 please take a look at the picture to c the result

result sampel
ethar1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike EghtebasDatabase and Application DeveloperCommented:
It will help if you provide data in CASES table.

try:
select *
from 
(
  select CaseID, CaseDate, CaseTypeID
  from #CASES
) s
pivot
(
  sum(CaseTypeID)
  for CaseDate in ([20060516], [20060517])
) t;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
This should work for a month only. You just need to change the month in the WHERE clause:
select *
from 
	(select Day(CaseDate) as MonthDay, CaseID, CaseTypeID
	 from CASES
	 where MONTH(CaseDate)=5) as t
pivot
(count(CaseID)
  for MonthDay in ([1],[2],[3],[4],[5],[6],[7],[8],[9]
		,[10],[11],[12],[13],[14],[15],[16],[17],[18],[19]
		,[20],[21],[22],[23],[24],[25],[26],[27],[28],[29]
		,[30],[31])) as pvt

Open in new window

ethar1Author Commented:
Thanks guys for your reply,

I need ([20060516], [20060517]) to be dynamic
Mike EghtebasDatabase and Application DeveloperCommented:
CaseDate domain could include years and months. What will be your search criteria? All days in  month?

What are some examples of your start and end dates?

Question: Did my solution work (though for two days)?

Mike
ethar1Author Commented:
Yes your solution work , but I need dynamic cols
any idea instead of :
select @cols = STUFF((SELECT ',' + QUOTENAME(Convert(varchar(4),MemberID))  +'' FROM Members FOR XML PATH ('') ),1,1,'')
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.