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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:49:12+00:00 2026-05-28T22:49:12+00:00

I have a query: (using a very simple example) SELECT ItemCategory, ItemID, Sales FROM

  • 0

I have a query: (using a very simple example)

SELECT
    ItemCategory,
    ItemID,
    Sales
FROM
    MyTable
WHERE
    CustomerID = 1

Which returns

ItemCategory | ItemID | Sales
A              0        75.00    
A              1        50.00
A              2         0.00
B              3        25.00
B              4        25.00
C              5        20.00 
C              6        30.00
C              7        10.00
C              8         0.00
C              9        50.00

How can I modify this query so that I receive the percentage of sales for each item grouped by ItemCategory?

That is, I would like this returned:

ItemCategory | ItemID | Sales  | PercentageOfCategory
A              0        75.00    60%
A              1        50.00    40%
A              2         0.00     0%
B              3        25.00    50%
B              4        25.00    50%
C              5        20.00    20%
C              6        30.00    30%
C              7        10.00    10%
C              8         0.00     0%
C              9        50.00    50%

I tried to keep the example as trivial as possible, the actual query is pretty complex but I imagine the same logic still applies.

EDIT: I believe the server is sql server 2008

  • 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-28T22:49:13+00:00Added an answer on May 28, 2026 at 10:49 pm

    Assuming SQL Server 2005+ for the CTE:

    WITH cteCategoryTotals AS (
        SELECT ItemCategory, SUM(Sales) AS TotalSales
            FROM MyTable
            WHERE CustomerID = 1
            GROUP BY ItemCategory)
    SELECT m.ItemCategory, m.ItemId, m.Sales, (m.Sales/c.TotalSales)*100.0 AS PercentageOfCategory
        FROM MyTable m
            INNER JOIN cteCategoryTotals
                ON m.ItemCategory= c.ItemCategory
        WHERE m.CustomerID = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'll use this simple query as an example: Select MyColumn From MyTable Where PrimaryKeyColumn
I have a very simple table and I can happily query it using LINQ
I have a simple SQL query (using SqlCommand, SqlTransaction) in .NET 2.0 that returns
I have a very simple mapping which looks like this (I streamlined the example
I have a very simple query that is not much more complicated than: select
I have a very simple query (three where conditions; two equals, one between) from
I have a query in Delphi using DBExpress TSQLQuery that looks like so ActiveSQL.sql.add('SELECT
I have a problem with the following Linq query using Entity Framework: from o
I have a very simple problem which requires a very quick and simple solution
I have an expensive query using the row_number over() functionality in SQL Server 2005.

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.