Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8461805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:58:45+00:00 2026-06-10T13:58:45+00:00

My dataset looks like this: COLA | COLB Name1 | 218 Name2 | 157

  • 0

My dataset looks like this:

COLA  | COLB
Name1 | 218
Name2 | 157
Name3 | 134
Name4 | 121

I need this output:

COLA  | COLB| COLC
Name1 | 218 | 0.34
Name2 | 157 | 0.60
Name3 | 134 | 0.71
Name4 | 121 | 1

My SQL looks like this so far:

SELECT COLA, COLB, COLB/SUM(COLB) FROM #MyTempTable

Two problems with this SQL. One, COLC is 0 everytime and I don’t understand that. Two, even if it did result in the % it’s not a cumulative %.

I’ve seen some similar threads on StackOverflow, but I wasn’t able to make the answers from those threads work in my exact scenario.

Thanks in advance for any suggestions!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T13:58:46+00:00Added an answer on June 10, 2026 at 1:58 pm

    I think you’re looking for something like this, though your example calculations may be off a little:

    SELECT
        COLA,
        COLB,
        ROUND(
            -- Divide the running total...
            (SELECT CAST(SUM(COLB) AS FLOAT) FROM #MyTempTable WHERE COLA <= a.COLA) /
            -- ...by the full total
            (SELECT CAST(SUM(COLB) AS FLOAT) FROM #MyTempTable),
            2
        ) AS COLC
    FROM #MyTempTable AS a
    ORDER BY COLA
    

    EDIT: I’ve added rounding.

    This gives us the following output:

    COLA    COLB    COLC
    Name1   218     0.35
    Name2   157     0.6
    Name3   134     0.81
    Name4   121     1
    

    The reason that your results are 0 (or 1) is because you are dividing ints by ints, thus giving you an int (see Datatype precedence).

    UPDATE:

    I should add that this uses a “triangular join” to get the running total (WHERE COLA <= a.COLA). Depending upon your SQL Server version, you may compare this to other options if performance becomes a concern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset that looks like: ColA ColB ColC ColD ColE rs778 C
The XML dataset file after I add it to a DataSet looks like this:
I am trying to read a dataset which looks like this DATE,TIME,val 1/1/2001,1:00:00,0 with
i have a dataset that looks like this: Bin Frequency 6.0 0 5.9 0
I have code that looks like this: var ds = new DataSet(); var fooIDToFoo
I have a dataset that looks like this: a <- data.frame(rep(1,5),1:5,1:5) b <- data.frame(rep(2,5),1:5,1:5)
I have a dataset that looks like this: 0 _ _ 23.0186E-03 10 _
I have a SAS dataset that looks like this: var _12 _41 _17 12
Suppose I have a SAS dataset that looks like this: id x 1 1234
SQL 2008 Basically i have a dataset that looks like this: AcctID AcctType AcctSubType

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.