Trouble creating cross tab report SQL Server Reporting Services

Hi,

I'm retreiving monthly totals for a year from a table using sql with the results being displayed as shown below:


Totals     Month
2            1
4            2
8            3
3            4
3            5
8            6
2            7
7            8
2            9

I want the results to be displayed as follows in a cross-tab manner with the month being the heading and the total falling under it's correpsonding month

i.e.
           Jan   Feb   Mar   Apr   May   Jun   Jul    Aug   Sep   Oct   Nov  Dec  
2004      2      4      8      3      8      2      7      2      0      0      0      0

What I've got so far is in the sql shown below but it displays the results as follows:

          Jan   Feb   Mar   Apr   May   Jun   Jul    Aug   Sep   Oct   Nov  Dec  
2004      2      0      0      0      0      0      0      0      0      0      0      0
2004      0      4      0      0      0      0      0      0      0      0      0      0
2004      0      0      8      0      0      0      0      0      0      0      0      0
2004      0      0      0      3      0      0      0      0      0      0      0      0
2004      0      0      0      0      3      0      0      0      0      0      0      0
2004      0      0      0      0      0      8      0      0      0      0      0      0
.................................................................................................................
.................................................................................................................

Here's the sql:

SELECT year(CreateDate),
CASE month(CreateDate) WHEN 1 THEN Count(Distinct ID) ELSE 0 END AS Jan,
CASE month(CreateDate) WHEN 2 THEN Count(Distinct ID) ELSE 0 END AS Feb,
CASE month(CreateDate) WHEN 3 THEN Count(Distinct ID) ELSE 0 END AS Mar,
CASE month(CreateDate) WHEN 4 THEN Count(Distinct ID) ELSE 0 END AS Apr,
CASE month(CreateDate) WHEN 5 THEN Count(Distinct ID) ELSE 0 END AS May,
CASE month(CreateDate) WHEN 6 THEN Count(Distinct ID) ELSE 0 END AS Jun,
CASE month(CreateDate) WHEN 7 THEN Count(Distinct ID) ELSE 0 END AS Jul,
CASE month(CreateDate) WHEN 8 THEN Count(Distinct ID) ELSE 0 END AS Aug,
CASE month(CreateDate) WHEN 9 THEN Count(Distinct ID) ELSE 0 END AS Sep,
CASE month(CreateDate) WHEN 10 THEN Count(Distinct ID) ELSE 0 END AS Oct,
CASE month(CreateDate) WHEN 11 THEN Count(Distinct ID) ELSE 0 END AS Nov,
CASE month(CreateDate) WHEN 12 THEN Count(Distinct ID) ELSE 0 END AS Dec
from Document
where DocumentTypeId = 4
and year(CreateDate) = year(getDate()) - 3
and PortalID = 2
Group By year(CreateDate), month(CreateDate)


Any help/suggestion would be greatly appreciated.

Cheers




LVL 2
GlobexCorpAsked:
Who is Participating?
 
chiragkhabariaCommented:
Declare @Table Table
(
      Totals int,
      [Month] int
)

Insert @Table
Select 2,1 Union all
Select 4,2 Union all
Select 8,3 Union all
Select 3,4 Union all
Select 3,5 Union all
Select 8,6 Union all
Select 2,7 Union all
Select 7,8 Union all
Select 2,9

Select Sum( Case When Month = 1 Then Totals Else 0 End ) As Jan,
       Sum( Case When Month = 2 Then Totals Else 0 End ) As Feb,
       Sum( Case When Month = 3 Then Totals Else 0 End ) As March,
       Sum( Case When Month = 4 Then Totals Else 0 End ) As April,
       Sum( Case When Month = 5 Then Totals Else 0 End ) As May,
       Sum( Case When Month = 6 Then Totals Else 0 End ) As June,
       Sum( Case When Month = 7 Then Totals Else 0 End ) As July,
       Sum( Case When Month = 8 Then Totals Else 0 End ) As Aug,
       Sum( Case When Month = 9 Then Totals Else 0 End ) As Sept,
       Sum( Case When Month = 10 Then Totals Else 0 End ) As Oct,
       Sum( Case When Month = 11 Then Totals Else 0 End ) As Nov,
       Sum( Case When Month = 12 Then Totals Else 0 End ) As Decs
From @Table
0
 
chiragkhabariaCommented:
have a look at this query ..

Create table Tmp
(
      TransDate       int,
      Amount            int
)
insert into Tmp
select      2006001, 1000      union all
select      2006001, 1200      union all
select      2006001, 1300      union all
select      2006002, 1200      union all
select      2006002, 2600      union all
select      2006002, 9800      union all
select      2006002, 3540      union all
select      2006003, 1200      union all
select      2006003, 2200      union all
select      2006003, 3982      union all
select      2006003, 1200

--Select * From @Table

Declare @sQry Varchar(8000)

Declare @date int
Select @Date = 0,@sQry = 'Select * , '

While @Date < (Select Distinct Max(TransDate) From  Tmp )
Begin
Select @Date = Min(TransDate) From Tmp Where TransDate > @Date
Select @sQry = @sQry + ' Case When TransDate = ' + Convert(varchar,@Date) +  ' Then Amount Else 0 End As [' + Convert(varchar,@Date) + '],'
End

Select @sQry = left(@sQry,len(@sQry)-1)
Select @sQry = @sQry + ' From Tmp'
Exec(@sQry)

Drop Table Tmp

0
 
GlobexCorpAuthor Commented:
Thanks for the reply chiragkhabaria

I'm creating a temp table and then trying to read from that temp table and group by year but I get an error when I try to group by year alone. Any idea what's wrong?

-- CREATE TEMP TABLE
insert into Tmp
Select Count(Distinct ID) As Total , month(CreateDate) As Month, year(CreateDate) As Year
from Document Doc
where DocumentTypeId = 4
and year(CreateDate) = year(getDate()) - 3
and PortalID = 2
Group By month(CreateDate), year(CreateDate)
Order By month(CreateDate) Asc

-- SELECTING FROM TEMP TABLE

SELECT Year, Total,
CASE Month WHEN 1 THEN Total ELSE 0 END AS Jan,
CASE Month WHEN 2 THEN Total ELSE 0 END AS Feb,
CASE Month WHEN 3 THEN Total ELSE 0 END AS Mar,
CASE Month WHEN 4 THEN Total ELSE 0 END AS Apr,
CASE Month WHEN 5 THEN Total ELSE 0 END AS May,
CASE Month  WHEN 6 THEN Total ELSE 0 END AS Jun,
CASE Month WHEN 7 THEN Total  ELSE 0 END AS Jul,
CASE Month WHEN 8 THEN Total ELSE 0 END AS Aug,
CASE Month WHEN 9 THEN Total ELSE 0 END AS Sep,
CASE Month WHEN 10 THEN Total ELSE 0 END AS Oct,
CASE Month WHEN 11 THEN Total ELSE 0 END AS Nov,
CASE Month WHEN 12 THEN Total ELSE 0 END AS Dec
from Tmp
where Year = year(getDate()) - 3
Group By Year, Total, Month
Order By Month Asc
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
GlobexCorpAuthor Commented:
Sorry, the select statement above looks like this:

SELECT Year, Total,
CASE Month WHEN 1 THEN Total ELSE 0 END AS Jan,
CASE Month WHEN 2 THEN Total ELSE 0 END AS Feb,
CASE Month WHEN 3 THEN Total ELSE 0 END AS Mar,
CASE Month WHEN 4 THEN Total ELSE 0 END AS Apr,
CASE Month WHEN 5 THEN Total ELSE 0 END AS May,
CASE Month  WHEN 6 THEN Total ELSE 0 END AS Jun,
CASE Month WHEN 7 THEN Total  ELSE 0 END AS Jul,
CASE Month WHEN 8 THEN Total ELSE 0 END AS Aug,
CASE Month WHEN 9 THEN Total ELSE 0 END AS Sep,
CASE Month WHEN 10 THEN Total ELSE 0 END AS Oct,
CASE Month WHEN 11 THEN Total ELSE 0 END AS Nov,
CASE Month WHEN 12 THEN Total ELSE 0 END AS Dec
from Tmp
where Year = year(getDate()) - 3
Group By Year
Order By Month Asc
0
 
chrismcCommented:
You've mentioned Reporting Services in the heading, if you want to show this as a cross tab in RS, just use a Matrix control, it will work it out for you. No fancy SQL necessary.

Your query will be;

SELECT
        YearCreated = year(CreateDate),
        MonthCreated = month(CreateDate),
        RecordsCreated = Count(Distinct ID)
    From Document
    Where DocumentTypeId = 4
        And year(CreateDate) = year(getDate()) - 3
        And PortalID = 2
    Group By year(CreateDate), month(CreateDate)

Drag a matrix control onto the report
From the field list, drag the YearCreated field into the bottom left hand cell of the matrix (the Row group header).
Drag the MonthCreated field into the top right hand cell on the matrix (the column group header)
Drag the RecordsCreated field into the bottom right hand cell (the Data cell)
That's it! Job done!

Well actually, you'll probably want to tidy up the formatting but otherwise it's done.

Also if you really must get the query to produce a cross tab instead, consider using the new PIVOT command in SQL 2005. It will be easier than all those CASE statements.

Cheers
Chris
0
 
GlobexCorpAuthor Commented:
Thanks for the help!
0
 
chrismcCommented:
Hi GlobexCorp,
You never did say whether you were displaying this cross tab on a report or not? If you are, then I strongly suggest you use a matrix.

I'm not quibbling the points - that's the solution you wanted so that's fair enough. I just like to point out a better way to use RS.

Cheers
Chris
0
 
GlobexCorpAuthor Commented:
Hi chrismc,

I am using the cross tab on a report. I'm actually thinking of using the matrix now which means your solution is the one I should have accepted which I do apologise for. The temp table method is trickier.

Thanks for the help.
0
 
chrismcCommented:
No problem.

Cheers
Chris
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.