Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# SQL - How to do percent to total for each groups of record

Posted on 2012-03-26
Medium Priority
498 Views
I have the following dataset

Fund |  FundID | Unit | Price | Mkt Value
----------------------------------------------------------
ABC        123             1         10               10
ABC        123             1         20               20
ABC         456            1         30               30
DEF          456            1         40               40
DEF          789            1         50               50
XYZ          789            1         60               60
XYZ          123            1         70               70
XYZ          456            1         80               80

I would like to manipulate to get

FundID  | Subtotal |Percent Total
-------------------------------------------------
123               100               27.78
456               150               41.67
789               110               31.55

Can it possible to write a SQL statement to do this?
0
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
• 2

LVL 75

Expert Comment

ID: 37769502
Something like this:

``````;WITH    MyCTE
AS (SELECT    FundID,
SUM(Price) SubTotal
FROM      YourTable
GROUP BY  FundID
)
SELECT  c1.FundID,
c1.SubTotal,
(c1.SubTotal * 100.0) / c2.Total
FROM    MyCTE c1
CROSS JOIN (SELECT  SUM(SubTotal) Total
FROM    MyCTE
) c2
``````
0

LVL 75

Accepted Solution

Anthony Perkins earned 2000 total points
ID: 37769507
This is how I tested it:

``````DECLARE @Table TABLE
(
Fund char(3) NOT NULL,
FundID smallint NOT NULL,
Unit tinyint NOT NULL,
Price tinyint NOT NULL,
MktValue tinyint NOT NULL
)

SET NOCOUNT ON

INSERT  @Table
(Fund, FundID, Unit, Price, MktValue)
VALUES  ('ABC', 123, 1, 10, 10),
('ABC', 123, 1, 20, 20),
('ABC', 456, 1, 30, 30),
('DEF', 456, 1, 40, 40),
('DEF', 789, 1, 50, 50),
('XYZ', 789, 1, 60, 60),
('XYZ', 123, 1, 70, 70),
('XYZ', 456, 1, 80, 80);

WITH    MyCTE
AS (SELECT    FundID,
SUM(Price) SubTotal
FROM      @Table
GROUP BY  FundID
)
SELECT  c1.FundID,
c1.SubTotal,
(c1.SubTotal * 100.0) / c2.Total
FROM    MyCTE c1
CROSS JOIN (SELECT  SUM(SubTotal) Total
FROM    MyCTE
) c2
``````

Output:
FundID      SubTotal      (No column name)
123      100      27.777777777777
456      150      41.666666666666
789      110      30.555555555555
0

LVL 25

Expert Comment

ID: 37771038
And when you want it to 2 decimals.

ROUND ( ((c1.SubTotal * 100.0) / c2.Total), 2 ) As pct
0

Author Closing Comment

ID: 37772931
Thank you very much.
0

## Featured Post

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
###### Suggested Courses
Course of the Month4 days, 8 hours left to enroll