Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 How to return a certain percentage of rows (NOT TOP PERCENT QUESTION) Rate Topic Display Mode Topic Options
Author
 Message
 Posted Wednesday, July 1, 2009 9:31 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 5, 2012 9:12 AM Points: 293, Visits: 459
 I want to return all values that represent x% of the total values within a table. Here's some test data:`create table #temp (customer varchar(15),number int)insert into #temp select'A',10insert into #temp select'B',20insert into #temp select'C',17insert into #temp select'D',18insert into #temp select'E',30insert into #temp select'F',40insert into #temp select'G',10insert into #temp select'H',20insert into #temp select'I',17insert into #temp select'J',18insert into #temp select'K',30insert into #temp select'L',40select top 60 percent * from #temporder by number descdrop table #temp`When this is executed 60% of the total lines are returned but what I want is for it to return those rows whose 'number' values represent 60% of the total when added. So using my test data, 60% of the total would be represented by customers F, L, K and E because the sum of all the rows is 233 and the sum of F, L, K and E is 140 = 60%ThanksDavid
Post #745525
 Posted Wednesday, July 1, 2009 10:34 AM
 Old Hand Group: General Forum Members Last Login: Tuesday, May 15, 2012 4:11 AM Points: 369, Visits: 2,661
 I'm a little confused - how would you differentiate between sum(F, L, K) and sum (A, B, E, F, L) which also adds up to 140? ------------------------------------------------------------------------Bite-sized fiction (with added teeth)
Post #745599
 Posted Wednesday, July 1, 2009 10:47 AM
 Ten Centuries Group: General Forum Members Last Login: Tuesday, October 25, 2016 8:49 AM Points: 1,037, Visits: 432
 This is a weird one...would something like this do the trick?`declare @Total decimal(18,2)select @Total = sum(number)from #tempcreate table #temp2( customer varchar(15), number int, Ranking int)insert #temp2select customer, Number, ranking = row_number() over(order by number desc)from #tempselect customer, number, Running, pct = round((cast(Running as decimal(10,2))/ cast(@Total as decimal(10,2)))*100, 0)from( select customer, number, Ranking = ntile(12) over(order by number desc), Running = (select sum(number) from #temp2 t1 where t1.Ranking <= t.Ranking) from #temp2 t)subwhere round((cast(Running as decimal(10,2))/ cast(@Total as decimal(10,2)))*100, 0) <= 60drop table #temp2`This mess first orders the customers by Number Desc. Then calculates the running total and percentage of the running total against the total total, and returns those rows that add up to close to 60% without going over ("you're the next contestant on the Price is Right"...)This is based on an inference that you want to return the top 60% of customers by number.This works fine on a small set, but doubt it would be pretty on a large set of data...lots of table scans, sorts and temp table weirdness... /***************** If most people are not willing to see the difficulty, this is mainly because, consciously or unconsciously, they assume that it will be they who will settle these questions for the others, and because they are convinced of their own capacity to do this. -Friedrich August von Hayek *****************/
Post #745611
 Posted Wednesday, July 1, 2009 10:52 AM
 SSC Veteran Group: General Forum Members Last Login: Thursday, February 25, 2016 12:24 PM Points: 292, Visits: 1,657
 I wasn't sure if you were looking for all combinations of Customers that meet that percentage or just starting with the min number or.???At anyrate, I just used a simple running total solution to get some results. Hopefully, it'll help:`DECLARE @Temp table (customer varchar(15),number int)insert into @Temp select'A',10insert into @Temp select'B',20insert into @Temp select'C',17insert into @Temp select'D',18insert into @Temp select'E',30insert into @Temp select'F',40insert into @Temp select'G',10insert into @Temp select'H',20insert into @Temp select'I',17insert into @Temp select'J',18insert into @Temp select'K',30insert into @Temp select'L',40DECLARE @Sum INTSELECT @Sum = SUM(number)FROM @TempDECLARE @Percent FLOATSET @Percent = @Sum * .6SELECT customer, Number, RunningTotalFROM ( SELECT customer, number, ( SELECT SUM(Number) FROM ( SELECT *,ROW_NUMBER() OVER (ORDER BY Number, Customer) AS RowNum FROM @Temp ) AS A WHERE RowNum <= T.RowNum ) AS RunningTotal FROM (SELECT *,ROW_NUMBER() OVER (ORDER BY Number, Customer) AS RowNum FROM @Temp) T ) AS DWHERE RunningTotal <= @Percent -- Could add on some sort of varience if neededORDER BY RunningTotal`
Post #745617
 Posted Wednesday, July 1, 2009 1:26 PM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 5, 2012 9:12 AM Points: 293, Visits: 459
 Thanks all, I'm out of the office so I'll test your solutions tomorrow.Just to clarify, what I wanted was to first order the rows in the temp table by number (desc) and then count 60% from the top downwards. Once 60% has been reached, or as close to, then stop. I see that you've already guessed that though!
Post #745715
 Posted Thursday, July 2, 2009 9:35 AM
 SSC Veteran Group: General Forum Members Last Login: Monday, November 5, 2012 9:12 AM Points: 293, Visits: 459
 Ok, I have had a chance to test out your solutions and am glad to say that after a bit of fiddling it is returning the data I need! We have a mixed environment here where some databases are not SQL 2005 compatible so the row numbering had to be done "the old way". Nevertheless, it is working now so thank you for your help.
Post #746389
 Posted Wednesday, December 5, 2012 8:06 AM
 Forum Newbie Group: General Forum Members Last Login: Thursday, August 11, 2016 7:36 AM Points: 2, Visits: 71
 Hi David:Could you post your final solution? I have found out that two suggestions gave two different results and I would like to know the right one you have finally used.Thanks,Adrian
Post #1393032
 Posted Thursday, December 13, 2012 4:28 AM
 Ten Centuries Group: General Forum Members Last Login: Monday, October 3, 2016 5:19 AM Points: 1,144, Visits: 3,432
 `create table #temp (customer varchar(15),number int)insert into #temp select'A',1insert into #temp select'B',1insert into #temp select'C',1insert into #temp select'D',1drop table #temp`What is the expected results of selecting 25% of the above? The SQL Guy @ blogspot@SeanPearceSQLAbout Me
Post #1396111

 Permissions