I have create a procedure to retrive the result of MonthlyTotal for each customer on a region, but the months are generated depend on query condition. result looks like
Region | CustomerName | 2013-11 | 2013-12 | 2014-01 | 2014-02 | ||||
MonthlyTotal | Reduce Total | MonthlyTotal | Reduce Total | MonthlyTotal | Reduce Total | MonthlyTotal | Reduce Total | ||
Jiangsu | Restaurant | NULL | NULL | NULL | NULL | 3810 | 116 | 150 | 3.5 |
ShangHai | Market | 2200 | 2.5 | 200 | 23 | NULL | NULL | NULL | NULL |
ZheJiang | Market | 2200 | 2.5 | 200 | 23 | NULL | NULL | 1220 | 73 |
ZheJiang | Restaurant | NULL | NULL | NULL | NULL | NULL | NULL | 150 | 23 |
now I met the issue is: I want to display the results in Crystal Report, since the DataSet 'year-month' column will be generated in Runtime depend on the user input startdate/enddate, so I can't drag/drop the columns in Crystal Report when design. my proc code is:
ALTER PROCEDURE [dbo].[GetMonthlyTotal] -- Add the parameters for the stored procedure here @startDate date, @endDate date AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here if not object_id('tempdb..#a2') is null drop table #a2 declare @s nvarchar(4000) SELECT region,CustomerName,MonthlyTotal,CONVERT(char(7),recordDate,120) recordDate into #a2 FROM MonthlySaleTest where recordDate between @startDate and @endDate Select @s=isnull(@s+',','')+quotename([recordDate]) from #a2 group by [recordDate] exec('select * from #a2 pivot (sum([MonthlyTotal]) for [recordDate] in('+@s+'))b order by region,CustomerName') END
Is there a way to work around that can be display all the dataset data in crystal report wihch get from database.