Solved

Trouble creating cross tab report SQL Server Reporting Services

Posted on 2007-04-05
9
3,008 Views
Last Modified: 2012-05-05
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




0
Comment
Question by:GlobexCorp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 6

Expert Comment

by:chiragkhabaria
ID: 18858318
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
 
LVL 2

Author Comment

by:GlobexCorp
ID: 18859079
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
 
LVL 2

Author Comment

by:GlobexCorp
ID: 18859092
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:chrismc
ID: 18860651
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
 
LVL 6

Accepted Solution

by:
chiragkhabaria earned 500 total points
ID: 18861405
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
 
LVL 2

Author Comment

by:GlobexCorp
ID: 18862810
Thanks for the help!
0
 
LVL 18

Expert Comment

by:chrismc
ID: 18862836
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
 
LVL 2

Author Comment

by:GlobexCorp
ID: 18863030
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
 
LVL 18

Expert Comment

by:chrismc
ID: 18863108
No problem.

Cheers
Chris
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to increase the row limit in Jasper Server.
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question