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

 TSQL and percentile (not percentilerank :) Rate Topic Display Mode Topic Options
Author
 Message
 Posted Friday, December 28, 2012 11:19 AM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, December 6, 2016 1:53 PM Points: 36, Visits: 146
 Hi all,I would like to ask for some help and maybe the best practices on calculating the 90th percentile using TSQL. To be clear after searching online i'm looking for one value to show based on various numbers during a time frame. Example what is the 90th percentile for students duration of an exam for the week of July 1st.I believe i have figured out the formula below (which excludes NULLS)select max(case when rownum*1.0/numrows <= 0.9 then end) as percentile_90thfrom (select , row_number() over (order by ) as rownum, count(*) over (partition by NULL) as numrows from where is not null AND DATE = '12-17-2012' (etc) ) tThere are now 2 items I’m still working on to complete this query at my workplace and that is:1 – get 2 decimal places as result (right now the output has no decimal places)2 – integrate the Date to be dynamic (for last week for example) -> replace static with DATE >= CONVERT(date, GETDATE() -8)Before i go ahead and spend more time i wanted to see what the community has for the best method to calculate percentile? i did a spot check and extracted the raw data and found this calculation is right on except for 1 date range i used and i'm not sure why right now.Any advice is appreciated!
Post #1400991
 Posted Friday, December 28, 2012 1:36 PM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, December 6, 2016 1:53 PM Points: 36, Visits: 146
 so it seems like i spoke too soon, this formula only provides you with the row in the 90th percentile not the value.What i'm looking for is a 90th percentile value
Post #1401043
 Posted Friday, December 28, 2012 2:51 PM
 SSCoach Group: General Forum Members Last Login: Tuesday, December 6, 2016 8:08 PM Points: 16,145, Visits: 16,850
 If you can post some ddl, sample data and desired output you will find lots of people diving in to this one. Please see the first article in my signature for best practices when posting questions. _______________________________________________________________Need help? Help us help you. Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.Need to split a string? Try Jeff Moden's splitter.Cross Tabs and Pivots, Part 1 – Converting Rows to Columns Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs Understanding and Using APPLY (Part 1)Understanding and Using APPLY (Part 2)
Post #1401071
 Posted Monday, December 31, 2012 2:13 PM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, December 6, 2016 1:53 PM Points: 36, Visits: 146
 Thank you for the information. I would like to delete this topic as i have found my answer. After reading a bit it seems that we can't delete our own topics?
Post #1401505
 Posted Monday, December 31, 2012 2:19 PM
 SSC-Insane Group: General Forum Members Last Login: Yesterday @ 6:24 AM Points: 23,522, Visits: 37,765
 cs_source (12/31/2012)Thank you for the information. I would like to delete this topic as i have found my answer. After reading a bit it seems that we can't delete our own topics?Having asked a question and upon finding your own answer, it is actually good forum etiquette to post how you solved your problem. Others may come along with the same or similar question and your answer may help them.
Post #1401507
 Posted Tuesday, January 1, 2013 6:44 PM
 Hall of Fame Group: General Forum Members Last Login: Wednesday, February 24, 2016 6:28 AM Points: 3,977, Visits: 6,431
 Was your solution something like this?`;WITH Scores (Score, Percentile) AS ( SELECT Score, NTILE(10) OVER(ORDER BY Score) FROM sys.all_columns CROSS APPLY (SELECT 51+ ABS(CHECKSUM(NEWID())) % 50) a(Score) )SELECT ScoreFROM ScoresWHERE Percentile > 9` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Post #1401636
 Posted Thursday, January 3, 2013 11:11 AM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, December 6, 2016 1:53 PM Points: 36, Visits: 146
 Hi Lynn & Dwayne,I spent a few days trying to find an answer and found the closest value to be the first query i posted, however that query doesn't work for what i need. At this time i decided to go back to SAS to calculate the percentile, however i will be tackling this once i have a few things off my plate.If you like i can post the SAS code? :)
Post #1402483
 Posted Thursday, January 3, 2013 12:28 PM
 SSCommitted Group: General Forum Members Last Login: Wednesday, November 30, 2016 11:24 AM Points: 1,579, Visits: 2,475
 cs_source (1/3/2013) If you like i can post the SAS code? :)Not the code for percentile, but if you can figure out a way to use OPENROWSET with the SAS OLE providers I would sure appreciate it. I am able to use VBA in Excel to read SAS files, and to use the import wizard in SQL Server to import .sas7bdat files, but for the life of me I can't get OPENROWSET to work with the same SAS providers that Excel has no problem with. Greg_________________________________________________________________________________________________The glass is at one half capacity: nothing more, nothing less.
Post #1402505
 Posted Thursday, January 3, 2013 4:21 PM
 SSC Rookie Group: General Forum Members Last Login: Tuesday, December 6, 2016 1:53 PM Points: 36, Visits: 146
 At this time i only use SAS for the percentile not for any other function so your solution is one step further than mine. When i review this hopefully within the next two weeks i'll focus on just getting this done in SQL.
Post #1402606
 Posted Thursday, January 3, 2013 6:47 PM
 SSCommitted Group: General Forum Members Last Login: Wednesday, November 30, 2016 11:24 AM Points: 1,579, Visits: 2,475
 cs_source (1/3/2013)At this time i only use SAS for the percentile not for any other function so your solution is one step further than mine. When i review this hopefully within the next two weeks i'll focus on just getting this done in SQL.Well, while I'm not intending to steer anyone away from SQL Server, as I use it for 95% of handling data, if you have a license for SAS, you would be well served to start learning how to program in it, as it is an extremely powerful tool, and because of the exorbitant annual license fee, most folks don't have access to it. It is a good thing to be able to put on your resume. Greg_________________________________________________________________________________________________The glass is at one half capacity: nothing more, nothing less.
Post #1402634

 Permissions