Solved

Select bottom 25%

Posted on 2014-12-16
6
103 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 33

Expert Comment

by:Mike Eghtebas
Comment Utility
You can order ASC and Select Top 25%
0
 

Author Comment

by:Scarlett72
Comment Utility
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 33

Assisted Solution

by:Mike Eghtebas
Mike Eghtebas earned 100 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

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

Accepted Solution

by:
Jim Horn earned 300 total points
Comment Utility
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:ScottPletcher
ScottPletcher earned 100 total points
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now