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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:04:31+00:00 2026-06-01T13:04:31+00:00

I have a MSSQL table stores that has the following columns in a table:

  • 0

I have a MSSQL table stores that has the following columns in a table:

Storeid, NumEmployees
1       125 
2       154
3       10 
4       698
5       54  
6       98
7       87
8       100
9       58
10      897

Can someone help me with the SQL query to produce the top stores(storeID) that has 30% of the total emplyees(NumEmployees)?

  • 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-01T13:04:32+00:00Added an answer on June 1, 2026 at 1:04 pm
    WITH cte 
         AS (SELECT storeid, 
                    numemployees, 
                    ( numemployees * 100 ) / SUM(numemployees) OVER (PARTITION BY 1) 
                    AS 
                    percentofstores 
             FROM   stores) 
    SELECT * 
    FROM   cte 
    WHERE  percentofstores >= 30 
    ORDER BY numemployees desc
    

    Working Demo

    Alternative that doesn’t use SUM/OVER

    SELECT s.storeid, s.numemployees 
    FROM   (SELECT SUM(numemployees) AS [tots] 
            FROM   stores) AS t, 
           stores s 
    WHERE  CAST(numemployees AS DECIMAL(15, 5)) / tots >= .3 
    ORDER BY s.numemployees desc
    

    Working Demo

    Note that in the second version I decided not to multiply by 100 before dividing. This requires a cast to decimal otherwise it would be implicitly converted to a int resulting in no records returned

    Also I’m not completely clear that you want this, but you can add TOP 1 to both queries and it will limit the results to just the one with the greatest # of stores with more than 30%

    UPDATE

    Based on your comments it sounds to paraphrase Kevin

    You want the rows, starting at the store with the most employees and working down until you have at least 30 %

    This is difficult because it requires a running percentage and its a bin packing problem however this does work. Note I’ve included two other test cases (where the percent exactly equals and its just over the top two combined)

    Working Demo

    DECLARE @percent DECIMAL (20, 16) 
    
    SET @percent = 0.3
    --Other test values
    --SET @percent = 0.6992547128452433
    --SET @percent = 0.6992547128452434 
    
    ;WITH sums 
         AS (SELECT DISTINCT s.storeid, 
                             s.numemployees, 
                             s.numemployees + Coalesce(SUM(s2.numemployees) OVER ( 
                                                       PARTITION 
                                                       BY 
                                                       s.numemployees), 0) 
                             runningsum 
             FROM   stores s 
                    LEFT JOIN stores s2 
                      ON s.numemployees < s2.numemployees), 
         percents 
         AS (SELECT storeid, 
                    numemployees, 
                    runningsum, 
                    CAST(runningsum AS DECIMAL(15, 5)) / tots.total 
                    running_percent, 
                    Row_number() OVER (ORDER BY runningsum, storeid ) rn 
             FROM   sums, 
                    (SELECT SUM(numemployees) total 
                     FROM   stores) AS tots) 
    SELECT p.storeID,
           p.numemployees,
           p.running_percent,
           p.running_percent,
           p.rn 
    FROM   percents p 
           CROSS JOIN (SELECT MAX(rn) rn 
                      FROM   percents 
                      WHERE  running_percent = @percent) exactpercent 
    
           LEFT JOIN (SELECT MAX(rn) rn 
                       FROM   percents 
                       WHERE  running_percent <= @percent) underpercent 
             ON p.rn <= underpercent.rn 
                 OR ( exactpercent.rn IS NULL 
                      AND p.rn <= underpercent.rn + 1 ) 
    WHERE 
          underpercent.rn is not null or p.rn = 1       
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an table in my MySql database that has the following columns: -
I have mysql table that has a column that stores xml as a string.
I have a Mysql table that stores dates in the format (YYYY-MM-DD HH:MM:SS). What
I have the following problem: Our system has products that when released only are
I have a MySQL table pedigree that stores all my interconnecting parentage data as
I have a MySQL database table that has a VARCHAR field storing a date
Suppose I have this database table (some sample code below) that stores the relationship
I have a table that stores customer items. The table needs to reflect the
I have an ecommerce MySQL database that stores order information in the table 'orders'
I have a text file which has the following structure: 341|18 Hello world|20090225230048AAnhStI|90|$0.30|10|289|2|2|2|Is that

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.