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

  • SEARCH
  • Home
  • 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 7537901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:58:27+00:00 2026-05-30T06:58:27+00:00

I have an SQL Server table Table_1 with name, subname, value columns For each

  • 0

I have an SQL Server table Table_1 with name, subname, value columns

For each subname i can get the average of the values by the query:

select subname, AVG(value) as avg from Table_1 group by subname;

I also can get the sum of values for each name:

select name, sum(value) as sum from Table_1 group by name;

I need the percentage of each avg row for each of the name, i cant figure this out.

EDIT: Eg. of percent would be — average val1 is x% of the total avg values in a1

select * from Table_1 looks like:

name subname value  
a1  val1    20  
a1  val2    20  
a1  val3    10  
a1  val1    40  
a1  val2    30  
a2  2val3   90   
a2  2val1   80  
a2  2val2   30  
a2  2val3   90   
a2  2val1   80  
a2  2val1   630  
  • 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-05-30T06:58:28+00:00Added an answer on May 30, 2026 at 6:58 am
    WITH data (name, subname, value) AS (
      SELECT 'a1',  'val1 ',   20  UNION ALL
      SELECT 'a1',  'val2 ',   20  UNION ALL
      SELECT 'a1',  'val3 ',   10  UNION ALL
      SELECT 'a1',  'val1 ',   40  UNION ALL
      SELECT 'a1',  'val2 ',   30  UNION ALL
      SELECT 'a2',  '2val3',   90  UNION ALL 
      SELECT 'a2',  '2val1',   80  UNION ALL
      SELECT 'a2',  '2val2',   30  UNION ALL
      SELECT 'a2',  '2val3',   90  UNION ALL 
      SELECT 'a2',  '2val1',   80  UNION ALL
      SELECT 'a2',  '2val1',   630  
    )
    SELECT
      name,
      subname,
      percentage = AVG(value) * 100 / SUM(AVG(value)) OVER (PARTITION BY name)
    FROM data
    GROUP BY
      name,
      subname
    ;
    

    Output:

    name subname percentage
    ---- ------- -----------
    a1   val1    46
    a1   val2    38
    a1   val3    15
    a2   2val1   68
    a2   2val2   7
    a2   2val3   23
    

    Useful reading:

    • OVER Clause (Transact-SQL)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table Customer with 2 columns Name and Surname (sql Server 2008).
We have a SQL Server table containing Company Name, Address, and Contact name (among
I have a SQL Server table with 2 columns, Code and CodeDesc. I want
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
I have a table in SQL Server which looks like this: ID Code Name
I have three table in SQL Server 2008: Students StudentId Name 1 Ghanshyam 2
HI, Using Microsoft SQL Server 2005: I have a table test: id, link_id, name
I have a SQL Server Table like below: S.No ID Name ------- ----- -------
I have following table (SQL Server) Table name is LandParcels Blockid ParcelNo Stateorprivate ========================
I have a SQL Server 2005 table called Users: UserID | Date | Name

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.