• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 121
  • Last Modified:

Select bottom 25%

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
Scarlett72
Asked:
Scarlett72
3 Solutions
 
Mike EghtebasDatabase and Application DeveloperCommented:
You can order ASC and Select Top 25%
0
 
Scarlett72Author Commented:
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
 
Mike EghtebasDatabase and Application DeveloperCommented:
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
Mike EghtebasDatabase and Application DeveloperCommented:
Please note that my last post has been revised.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
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
 
Scott PletcherSenior DBACommented:
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 recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now