Solved

SQL

Posted on 2012-04-01
7
288 Views
Last Modified: 2012-04-02
I have a stored procedure  (SP1) that return the following table.
for example ,   exec SP1 32111
Risk      Amount
1      NULL
2      0.00
3      15282.95
4      33159.88
5      0.00
6      0.00

I will like to turn the Amount into Percent (by totalizing the Amount columns)
Then I will like to insert the result  as a record into a table in this format.  How do I do that
SPParm |  1       | 2       | 3         | 4         | 5         | 6        |
-----------------------------------------------------------------------------
32111        0           0         31.55     68.45     0            0


I am runing SQL Server 2003
0
Comment
Question by:tommym121
[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
7 Comments
 
LVL 39

Expert Comment

by:appari
ID: 37794368
can you post the existing sql from the stored procedure?
You can do it using pivot operator, check the following for an idea
declare @tab table(Risk  int,    Amount numeric(10,2))

insert into @tab
Select 1,      NULL
union select 2,      0.00
union select 3,      15282.95
union select 4,      33159.88
union select 5,      0.00
union select 6,      0.00

;
SELECT 32111 SPPARAM,[1], [2], [3], [4], [5],[6]
FROM
(SELECT Risk, isnull(Amount,0)/ Total * 100.0 TOT
    FROM @tab, (Select Sum(Amount) Total from @tab) A) AS SourceTable
PIVOT
(
sum(TOT)
FOR RISK IN ([1], [2], [3], [4], [5],[6])
) AS PivotTable

Open in new window

0
 

Author Comment

by:tommym121
ID: 37794443
Is there anyway to do  without pivot.  SQL server 2003 does not support PIVOT
0
 
LVL 39

Expert Comment

by:appari
ID: 37794453
then try like this
declare @tab table(Risk  int,    Amount numeric(10,2))

insert into @tab
Select 1,      NULL
union select 2,      0.00
union select 3,      15282.95
union select 4,      33159.88
union select 5,      0.00
union select 6,      0.00

;
SELECT SPPARAM, 
sum(case when Risk=1 then AMOUNT else 0 end )/sum(Amount) * 100.0 [1], 
sum(case when Risk=2 then AMOUNT else 0 end )/sum(Amount) * 100.0 [2], 
sum(case when Risk=3 then AMOUNT else 0 end )/sum(Amount) * 100.0 [3], 
sum(case when Risk=4 then AMOUNT else 0 end )/sum(Amount) * 100.0 [4], 
sum(case when Risk=5 then AMOUNT else 0 end )/sum(Amount) * 100.0 [5], 
sum(case when Risk=6 then AMOUNT else 0 end )/sum(Amount) * 100.0 [6]
FROM
(SELECT 32111 SPPARAM,Risk, isnull(Amount,0) AMOUNT
    FROM @tab) AS SourceTable
Group by SPPARAM

Open in new window

0
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:tommym121
ID: 37794522
appari,

Thanks.  How do I avoid 'Divide by zero'  if my Dataset like this
Risk      Amount
1      NULL
2      0.00
3      0.00
4      NULL
5      0.00
6      0.00

I will like to turn the Amount into Percent (by totalizing the Amount columns)
Then I will like to insert the result  as a record into a table in this format.  How do I do that
SPParm |  1       | 2       | 3         | 4         | 5         | 6        |
-----------------------------------------------------------------------------
32111        0           0           0            0            0            0
0
 
LVL 39

Accepted Solution

by:
appari earned 500 total points
ID: 37794530
try this
declare @tab table(Risk  int,    Amount numeric(10,2))

insert into @tab
Select 1,      NULL
union select 2,      0.00
union select 3,      0 --15282.95
union select 4,      0 --33159.88
union select 5,      0.00
union select 6,      0.00

;
SELECT SPPARAM, 
sum(case when Risk=1 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else 0 end * 100.0 [1], 
sum(case when Risk=2 then AMOUNT else 0 end )/case when sum(Amount)=0 then 1 else 0 end * 100.0 [2], 
sum(case when Risk=3 then AMOUNT else 0 end )/case when sum(Amount)=0 then 1 else 0 end * 100.0 [3], 
sum(case when Risk=4 then AMOUNT else 0 end )/case when sum(Amount)=0 then 1 else 0 end * 100.0 [4], 
sum(case when Risk=5 then AMOUNT else 0 end )/case when sum(Amount)=0 then 1 else 0 end * 100.0 [5], 
sum(case when Risk=6 then AMOUNT else 0 end )/case when sum(Amount)=0 then 1 else 0 end * 100.0 [6]
FROM
(SELECT 32111 SPPARAM,Risk, isnull(Amount,0) AMOUNT
    FROM @tab) AS SourceTable
Group by SPPARAM

Open in new window

0
 

Author Comment

by:tommym121
ID: 37795903
Thanks.  I find a mistake that  cause a divison by zero.   I change

/ case when sum(Amount)=0 then 1 else  0 end * 100.0 [1],
into
/ case when sum(Amount)=0 then 1 else  sum(Amount) end * 100.0 [1],


            sum(case when Risk=1 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount) end * 100.0 [1],
            sum(case when Risk=2 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount)  end * 100.0 [2],
            sum(case when Risk=3 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount)  end * 100.0 [3],
            sum(case when Risk=4 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount)  end * 100.0 [4],
            sum(case when Risk=5 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount)  end * 100.0 [5],
            sum(case when Risk=6 then AMOUNT else 0 end )/ case when sum(Amount)=0 then 1 else  sum(Amount)  end * 100.0 [6]
0
 

Author Closing Comment

by:tommym121
ID: 37798549
Thank you very much
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

617 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