Solved

Crosstab- sum of sums

Posted on 2004-09-15
20
455 Views
Last Modified: 2012-06-22
Hi,
I’m trying to create a report in sql server which basically is a crosstab, where it calculates fep% for each age group.
The table looks something like this:
Mytable:
PANEL      AGE            PAID             ISSUES
N1      10            1            1
N1      20            1            2
N1      16            1            1
P2      23            2            3
P2      29            1            1
P2      31            0            2

The final report should look like the following:

PANEL            AGE 8-21(FEP%)      AGE 22-35 (FEP%)      TOTAL      %
N1            75            0            75      60
P2            0            50            50      40
                  
            75            50            125            

FEP% is calculated for each age group and panel :
= sum(paid)/sum(issues) * 100

for example, For panel N1 : 3/4* 100

And finally total both rows and columns to calculate the final percentage.

I’m new to sql server and could create the age groups but I don’t know how to sum the sums and further on. Could this all be done in one query or a stored procedure is a better approach.  So far, the code for age groups:,
select      panel, sum(case when age between 8 and 21 then 1 else 0 end) as 'age 8-21',
      sum(case when age between 22 and 35 then 1 else 0 end) as 'age 22-35'
      
from       Mytable
group by panel


Can anybody help me please.

Many thanks,
Preeti

0
Comment
Question by:pmandair
  • 12
  • 7
20 Comments
 
LVL 10

Expert Comment

by:imrancs
ID: 12066031
select     panel, (sum(case when age between 8 and 21 then Paid else 0 end)/sum(case when age between 8 and 21 then Issued else 0 end)) * 100 as 'age 8-21(FEP%)',
     (sum(case when age between 22 and 35 then Paid else 0 end)/sum(case when age between 22 and 35 then Issued else 0 end)) * 100 as 'age 22-35(FEP%)',
(Sum(Paid)/Sum(Issued))/ 100 AS 'Total %'
from      Mytable
group by panel


Imran
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066068
please change the column name from issued to issues or as in  your table

0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066248
you may get the divided by zero error if there is a zero value in devisor

try this code

select     panel, (cast(sum(case when age between 8 and 21 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 8 and 21 then Issues else Null end) as numeric(10,2))) * 100.0 as 'age 8-21(FEP%)',
     (cast(sum(case when age between 22 and 35 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 22 and 35 then Issues else NULL end) as numeric(10,2))) * 100.0 as 'age 22-35(FEP%)',
Cast(Sum(Paid)as numeric(10,2))/Cast(Sum(Issues)as numeric(10,2))*100.0 'Total %'
from      Mytable
group by panel



Imran
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066317
the above code may have many zeros after the decimal point use cast them to numeric(10,2) like the following one


select     panel, cast((cast(sum(case when age between 8 and 21 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 8 and 21 then Issues else Null end) as numeric(10,2))) * 100 as numeric(10,2)) as 'age 8-21(FEP%)',
     cast((cast(sum(case when age between 22 and 35 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 22 and 35 then Issues else NULL end) as numeric(10,2))) * 100 as numeric(10,2))as 'age 22-35(FEP%)',
cast(Cast(Sum(Paid)as numeric(10,2))/Cast(Sum(Issues)as numeric(10,2))*100.0 as numeric(10,2)) as'Total %'
from      Mytable
group by panel


Imran
0
 
LVL 42

Expert Comment

by:EugeneZ
ID: 12066387
your report:


select     panel,
 cast(sum(case when age between 8 and 21 then 1 else 0 end) as varchar(50)) + '        ' +
   
cast(
  cast(
    (cast(sum(case when age between 8 and 21 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 8 and 21 then Issues else Null end) as numeric(10,2))) * 100.0
  as int)
as varchar(50))
as 'age 8-21(FEP%)',

 cast(sum(case when age between 22 and 35  then 1 else 0 end) as varchar(50)) + '        ' +
cast(
  cast(
   (cast(sum(case when age between 22 and 35 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 22 and 35 then Issues else NULL end) as numeric(10,2))) * 100.0
  as int)
as varchar(50))
as 'age 22-35(FEP%)',
cast(
Cast(Sum(Paid)as numeric(10,2))/Cast(Sum(Issues)as numeric(10,2))*100.0
as int)
'Total %'
from      Mytable
group by panel


0
 

Author Comment

by:pmandair
ID: 12066594
I'm sorry 'Total' and '%' are two separate columns in report. Total being the total for both the rows and columns. 60 in '%' is calcuated from (75/125)*100. The values are not aligned with the column names.
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066761
here is total column is added

select     panel, cast((cast(sum(case when age between 8 and 21 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 8 and 21 then Issues else Null end) as numeric(10,2))) * 100 as numeric(10,2)) as 'age 8-21(FEP%)',
     cast((cast(sum(case when age between 22 and 35 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 22 and 35 then Issues else NULL end) as numeric(10,2))) * 100 as numeric(10,2))as 'age 22-35(FEP%)',
Sum(IsNull(Paid,0) + IsNull(Issued,0)) As Total,
cast(Cast(Sum(Paid)as numeric(10,2))/Cast(Sum(Issues)as numeric(10,2))*100.0 as numeric(10,2)) as' %'
from      Mytable
group by panel



Imran
0
 

Author Comment

by:pmandair
ID: 12066790
Imran

You're nearly there...

0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066861
once again chage issued to issues :o)


0
 

Author Comment

by:pmandair
ID: 12066873
Could you calculate total on columns, for each age group?
0
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 10

Expert Comment

by:imrancs
ID: 12066891
please explain, may be with some example
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066937
you mean separate sum for Paid and Issues?
0
 

Author Comment

by:pmandair
ID: 12066944
NO, like I calculated in the report , for each age group then total i.e.125
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12066986
that would be a column and will repeat with each row, in your report you can place that column in the footer of the report. And that column i think is Total column that is calcualted as
Sum(IsNull(Paid,0) + IsNull(Issued,0)) As Total


BTW which reporting tool you are using, Crystal Reports ?


Imran
0
 

Author Comment

by:pmandair
ID: 12067018
That's the problem, we're not using any reporting tool. We're trying to do all the calculations if we could on the sql front before exporting it to excel through VB.net where all the formatting would be done. Also, some complicated calculations the ones that can't be handled in SQl.
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12067053
I think you have got your required output and remaining you can do int VB.NET.

If still there is any problem at any side SQL or VB.NET please ask.


Imran
0
 

Author Comment

by:pmandair
ID: 12067082
Also, one last thing is it possible to return 0 in place of Null.
0
 
LVL 10

Accepted Solution

by:
imrancs earned 500 total points
ID: 12067106
here is the revised version to convert NULLs to 0s

select     panel, IsNull(cast((cast(sum(case when age between 8 and 21 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 8 and 21 then Issues else Null end) as numeric(10,2))) * 100 as numeric(10,2)),0) as 'age 8-21(FEP%)',
     IsNull(cast((cast(sum(case when age between 22 and 35 then Paid else NULL end) as numeric(10,2))/cast(sum(case when age between 22 and 35 then Issues else NULL end) as numeric(10,2))) * 100 as numeric(10,2)),0) as 'age 22-35(FEP%)',
Sum(IsNull(Paid,0) + IsNull(Issues,0)) As Total,
IsNull(cast(Cast(Sum(Paid)as numeric(10,2))/Cast(Sum(Issues)as numeric(10,2))*100.0 as numeric(10,2)),0) as' %'
from      Mytable
group by panel



Imran
0
 

Author Comment

by:pmandair
ID: 12067125
Thank you!
0
 
LVL 10

Expert Comment

by:imrancs
ID: 12067146
You are Welcome!!!

Glad if could help.  :o)

Good Luck and happy reporting without reporting tool ;o)

Imran
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now