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 9134685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:42:27+00:00 2026-06-17T08:42:27+00:00

Suppose I had the following table in SQL Server: grp: val: criteria: a 1

  • 0

Suppose I had the following table in SQL Server:

grp:     val:     criteria:
a        1        1
a        1        1
b        1        1
b        1        1
b        1        1
c        1        1
c        1        1
c        1        1
d        1        1

Now what I want is to get an output which would basically be:

Select grp, val / [sum(val) for all records] grouped by grp where criteria = 1

So, given the following is true:

Sum of all values = 9
Sum of values in grp(a) = 2
Sum of values in grp(b) = 3
Sum of values in grp(c) = 3
Sum of values in grp(d) = 1

The output would be as follows:

grp:     calc:    
a        2/9        
b        3/9        
c        3/9        
d        1/9

What would my SQL have to look like??

Thanks!!

  • 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-17T08:42:28+00:00Added an answer on June 17, 2026 at 8:42 am

    You should be able to use something like this which uses sum() over():

    select distinct grp,
      sum(val) over(partition by grp)
        / (sum(val) over(partition by criteria)*1.0) Total
    from yourtable 
    where criteria = 1
    

    See SQL Fiddle with Demo

    The result is:

    | GRP |          TOTAL |
    ------------------------
    |   a | 0.222222222222 |
    |   b | 0.333333333333 |
    |   c | 0.333333333333 |
    |   d | 0.111111111111 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose i had the following string str = 'http://www.example.com/servlet/av/jd?ai=782&ji=2553120&sn=I' I want to get only
Suppose I had a MySQL table with the following column:    |variables| 1. | 'a'     | 2.
I'm quite new to LINQ. Suppose that I had the following table: Incident ID
For example, suppose I had written my own class which allowed me to record
Suppose that we have the following partial ER diagram: Notice that the attachments table
Suppose I had the following function: function alertMesg() { alert(This ok function alerts message!);
I have a table in database which has following structure(data) : +--------------------------+ Organization +--------------------------+
I did the following in MS Access: I made a form which had a
Suppose I had the following method in an object: public class foo { public
Suppose I had a file fonts.css.scss with the following: @font-face { font-family: 'Play'; font-style:

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.