sql server 2008 - SQL - How to return min and max values for each quintile -


in query selecting current balances of loans. created column returns quintile each loan balance falls into.

i used statement

ntile(5) on (order currloanbal) 

from here, how return min , max values each quintile? don't want group rows, want each row show min , max particular quintile.

you need use aggregate functions , partitioning

select s.col1, s.col2, ... s.coln,  max(s.col1) on (partition s.col2, ... ), min(s.col1) on (partition s.col2, ... ) stuff s; 

where partition clause decides how data partitioned ("grouped") without grouping same way group would. way window functions applied each partition give data want.

the columns in partition bit determine how rows partitioned (what separates 1 quintile in case).

see https://msdn.microsoft.com/en-us/library/ms189461.aspx more information.


Comments