Solved

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

Posted on 2012-03-26
4
466 Views
Last Modified: 2012-03-27
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
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
  • 2
4 Comments
 
LVL 75

Expert Comment

by:Anthony Perkins
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

Open in new window

0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 500 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

Open in new window


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

Expert Comment

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

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

Author Closing Comment

by:tommym121
ID: 37772931
Thank you very much.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Are triggers slow? 7 30
point in time restore in SQL server 26 54
ms sql + help with query 2 45
Search Text in Views 2 28
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

710 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