Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Select bottom 25%

Posted on 2014-12-16
6
Medium Priority
?
119 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 400 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
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 1200 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

647 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