Solved

Select bottom 25%

Posted on 2014-12-16
6
115 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 34

Expert Comment

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

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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

696 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