Solved

Select bottom 25%

Posted on 2014-12-16
6
110 Views
Last Modified: 2014-12-23
I am trying to find the bottom 25% value of the 'Value' column, broken up by MainGroup ideally broken into 4 quarters.
Any help would be appreciated.

IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL
DROP TABLE #mytable

CREATE TABLE #mytable
(
MainGroup varchar(50),
SubGroup varchar(50),
Value int
)

insert into #mytable

(MainGroup, SubGroup, Value)

select 'Temporary Assignments','Internal','783' union all
select 'Temporary Assignments','Internal','225' union all
select 'Temporary Assignments','Internal','118' union all
select 'Temporary Assignments','Internal','1014' union all
select 'Operation''s Support','Internal','2006' union all
select 'Operation''s Support','Internal','6352' union all
select 'Operation''s Support','Internal','186' union all
select 'Operation''s Support','Internal','186' union all
select 'Technical','external','186' union all
select 'Technical','external','367' union all
select 'Technical','external','142' union all
select 'Technical','external','46' union all
select 'Loyalty','external','5600' union all
select 'Loyalty','external','194' union all
select 'Loyalty','external','3679' union all
select 'Loyalty','external','48' union all
select 'Loyalty','external','5520' union all
select 'Technical','external','60' union all
select 'Technical','external','140' union all
select 'Loyalty','external','1287' union all
select 'Technical','external','261' union all
select 'Loyalty','external','5533' ;
with cte as
(
select *,
ROW_NUMBER() over(PARTITION by MainGroup order by value desc) as 'rn'
from #mytable
)
select * from cte

--the results would look like this ...
Loyalty      external      1287      5
Loyalty      external      194      6
Loyalty      external      48      7
Operation's Support      Internal      186      4
Technical      external      140      5
Technical      external      60      6
Technical      external      46      7
Temporary Assignments      Internal      118      4
0
Comment
Question by:Scarlett72
6 Comments
 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40503398
You can order ASC and Select Top 25%
0
 

Author Comment

by:Scarlett72
ID: 40503410
Hi eghtebas,
this gives me the bottom 25% of row_numbers, I need the bottom 25% based on the Value by the Main group.
0
 
LVL 34

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 100 total points
ID: 40503437
try

Select
      m.MainGroup
      , m.SubGroup
      , (Select Top 25% mm.Value From #mytable mm Order By mm.Value)
From #mytable m
Group By
      m.MainGroup
      , m.SubGroup


or

try

Select
      m.MainGroup
      , m.SubGroup
      , (Select Top 25% mm.Value From #mytable mm Order By mm.Value)
From #mytable m Inner Join #mytable mm
       on m.MainGroup=mm.MainGroup and m.SubGroup =mm.SubGroup
Group By
     m.MainGroup
      , m.SubGroup
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 34

Expert Comment

by:Mike Eghtebas
ID: 40503444
Please note that my last post has been revised.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 300 total points
ID: 40503590
Use NTILE to divvy the rows up into quarters, then pick the 4th quarter...
SELECT a.goo, a.foo, a.boo, a.quarters
FROM (
   SELECT goo, foo, boo, NTILE(4) OVER (PARTITION BY <columns that define what top 25% means) ORDER BY <sort order>) as quarters
   FROM your_Table) a
WHERE quarters = 4

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 100 total points
ID: 40503677
SELECT mt_top_25_percent.*    
FROM (
    SELECT DISTINCT MainGroup
    FROM #mytable
) AS mt_maingroup
CROSS APPLY (
    SELECT TOP (25) PERCENT *
    FROM #mytable mt
    WHERE
        mt.MainGroup = mt_maingroup.MainGroup
    ORDER BY value --or DESC?
) AS mt_top_25_percent
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question