Stephen Forte's Blog

 RSS/Feedburner
      Home     Steve & The Tank       

  Thursday, December 09, 2004

     SQL Server 2005-DENSE_RANK() and NTILE(n)

 

            DENSE_RANK works exactly like RANK() but will remove the skipping of numbers in the tie.  

 

Select SalesOrderID, CustomerID,

            DENSE_RANK() Over (Order By CustomerID) as RunningCount

From Sales.SalesOrderHeader

Where SalesOrderID>10000

Order By CustomerID

 

SalesOrderID CustomerID  RunningCount

------------ ----------- --------------------

43860        1           1

44501        1           1

45283        1           1

46042        1           1

46976        2           2

47997        2           2

49054        2           2

50216        2           2

51728        2           2

57044        2           2

63198        2           2

69488        2           2

44124        3           3

. . . More

 

NTile(n) will evenly divide all the results into approximately even pieces and assign each piece the same number in the resultset. A perfect example is the percent of 100 (like for an examination in University)  or a percentile of runners in a road race.

 

Select SalesOrderID, CustomerID,

            NTILE(10000) Over (Order By CustomerID) as RunningCount

From Sales.SalesOrderHeader

Where SalesOrderID>10000

Order By CustomerID

 

SalesOrderID CustomerID  RunningCount

------------ ----------- --------------------

43860        1           1

44501        1           1

45283        1           1

46042        1           1

46976        2           2

47997        2           2

49054        2           2

50216        2           2

51728        2           3

57044        2           3

63198        2           3

69488        2           3

44124        3           4

. . . More

 

One last example will bring these all together in one SQL Statement and show the difference between all four ranking functions.

 

--Ranking All

use adventureworks

Select SalesOrderID as OrderID, CustomerID,

            Row_Number() Over (Order By CustomerID) as RowNum,

            RANK() Over (Order By CustomerID) as Rank,

            DENSE_RANK() Over (Order By CustomerID) as DRank,

            NTILE(10000) Over (Order By CustomerID) as NTile

From Sales.SalesOrderHeader

Where SalesOrderID>10000

Order By CustomerID

 

 

OrderID     CustomerID  RowNum               Rank                 DRank             NTile  

----------- ----------- -------------------- -------------------- -------------------- --------------------

43860       1           1                    1                    1                    1

44501       1           2                    1                    1                    1

45283       1           3                    1                    1                    1

46042       1           4                    1                    1                    1

46976       2           5                    5                    2                    2

47997       2           6                    5                    2                    2

49054       2           7                    5                    2                    2

50216       2           8                    5                    2                    2

51728       2           9                    5                    2                    3

57044       2           10                   5                    2                    3

63198       2           11                   5                    2                    3

69488       2           12                   5                    2                    3

44124       3           13                   13                   3                    4

44791       3           14                   13                   3                    4

 

. . . More



SQL Server | Yukon Book

Thursday, December 09, 2004 1:34:28 PM (Eastern Standard Time, UTC-05:00)
Comments [2]  |  Trackback Related posts:
TSQL Enhancements in SQL Server 2008
Using a TSQL Common Table Expression to Find Dupe Records
Table Value Parameters Make Life Easier for C# Developers
TSQL 2008-Closer to C#
Programming Microsoft SQL Server 2008 is Out
SQL Server 2008 RC0-Lost a Few Hours
Tracked by:
"http://blastpr.com/wiki/js/pages/viagra/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/coumadin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/nexium/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/clomid/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/ultram/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/tramadol/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/soma/index.html" (http://morningside.e... [Pingback]
"http://morningside.edu/mics/_notes/pages/prilosec/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/prozac/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/cymbalta/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/viagra/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/soma/index.html" (http://blastpr.com/wiki/js/p... [Pingback]
"http://blastpr.com/wiki/js/pages/claritin/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://blastpr.com/wiki/js/pages/lipitor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/lexapro/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://morningside.edu/mics/_notes/pages/ultram/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/rainbow-brite/index.html" (http://blastpr.com/... [Pingback]
"http://blastpr.com/wiki/js/pages/hoodia/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://blastpr.com/wiki/js/pages/effexor/index.html" (http://blastpr.com/wiki/j... [Pingback]
"http://blastpr.com/wiki/js/pages/zoloft/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/cialis/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/cymbalta/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/celexa/index.html" (http://morningside... [Pingback]
"http://blastpr.com/wiki/js/pages/clomid/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/celebrex/index.html" (http://morningsi... [Pingback]
"http://blastpr.com/wiki/js/pages/synthroid/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blastpr.com/wiki/js/pages/cialis/index.html" (http://blastpr.com/wiki/js... [Pingback]
"http://morningside.edu/mics/_notes/pages/lipitor/index.html" (http://morningsid... [Pingback]
"http://morningside.edu/mics/_notes/pages/effexor/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/prilosec/index.html" (http://blastpr.com/wiki/... [Pingback]
"http://morningside.edu/mics/_notes/pages/tramadol/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/accutane/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/coumadin/index.html" (http://morningsi... [Pingback]
"http://morningside.edu/mics/_notes/pages/hoodia/index.html" (http://morningside... [Pingback]
"http://morningside.edu/mics/_notes/pages/synthroid/index.html" (http://mornings... [Pingback]
"http://morningside.edu/mics/_notes/pages/lexapro/index.html" (http://morningsid... [Pingback]
"http://blastpr.com/wiki/js/pages/melatonin/index.html" (http://blastpr.com/wiki... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/84238305/index.html" (http://blog.n... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/63224938/index.html" ... [Pingback]
"http://discussgod.com/cpstyles/docs/73291253/index.html" (http://discussgod.com... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/54089144/index.h... [Pingback]
"http://entartistes.ca/images/images/docs/81367526/index.html" (http://entartist... [Pingback]
"http://realestate.hr/templates/css/docs/36157459/index.html" (http://realestate... [Pingback]
"http://thebix.com/includes/compat/docs/51589391/index.html" (http://thebix.com/... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/04061117/index.html" (http://pspde... [Pingback]
"http://thejohnslater.com/pix/img/docs/42082955/index.html" (http://thejohnslate... [Pingback]
"http://legambitdufou.org/Library/docs/38152786/index.html" (http://legambitdufo... [Pingback]
"http://thebix.com/includes/compat/docs/44694113/index.html" (http://thebix.com/... [Pingback]
"http://witze-humor.de/templates/images/docs/83157240/index.html" (http://witze-... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/68291686/index.h... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/18769945/index.html" (http://pspde... [Pingback]
"http://swellhead.netswellhead.net/docs/79619129/index.html" (http://swellhead.n... [Pingback]
"http://split-dalmatia.com/split-dalmatia.com/images/docs/16705258/index.html" (... [Pingback]
"http://swellhead.netswellhead.net/docs/92808772/index.html" (http://swellhead.n... [Pingback]
"http://temerav.com/images/menu/96509501/index.html" (http://temerav.com/images/... [Pingback]
"http://swellhead.netswellhead.net/docs/42306518/index.html" (http://swellhead.n... [Pingback]
"http://pspdesktops.com/fileupload/store/docs/33460308/index.html" (http://pspde... [Pingback]
"http://allfreefilms.com/wp-includes/js/27702077/index.html" (http://allfreefilm... [Pingback]
"http://slaterjohn.com/downloads/2col/51579700/index.html" (http://slaterjohn.co... [Pingback]
"http://thejohnslater.com/pix/img/docs/73486930/index.html" (http://thejohnslate... [Pingback]
"http://witze-humor.de/templates/images/docs/69259068/index.html" (http://witze-... [Pingback]
"http://temerav.com/images/menu/20420171/index.html" (http://temerav.com/images/... [Pingback]
"http://realestate.hr/templates/css/docs/71546796/index.html" (http://realestate... [Pingback]
"http://discussgod.com/cpstyles/docs/62161481/index.html" (http://discussgod.com... [Pingback]
"http://add2rss.com/img/design/docs/90861918/index.html" (http://add2rss.com/img... [Pingback]
"http://pddownloads.com/docs/66275653/index.html" (http://pddownloads.com/docs/6... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/36483653/index.html" ... [Pingback]
"http://entartistes.ca/images/images/docs/28212733/index.html" (http://entartist... [Pingback]
"http://hrvatska.biz/wp-includes/js/docs/80692203/index.html" (http://hrvatska.b... [Pingback]
"http://swellhead.netswellhead.net/docs/84545083/index.html" (http://swellhead.n... [Pingback]
"http://plantmol.com/docs/24471383/index.html" (http://plantmol.com/docs/2447138... [Pingback]
"http://seo4u.at/images/docs/76783685/index.html" (http://seo4u.at/images/docs/7... [Pingback]
"http://slaterjohn.com/downloads/2col/28436634/index.html" (http://slaterjohn.co... [Pingback]
"http://blog.netmedia.hr/wp-includes/js/docs/91708760/index.html" (http://blog.n... [Pingback]
"http://lecouac.org/ecrire/lang/docs/20007231/index.html" (http://lecouac.org/ec... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/06712704/index.h... [Pingback]
"http://discussgod.com/cpstyles/docs/25383456/index.html" (http://discussgod.com... [Pingback]
"http://jivest2006.com/docs/42940613/index.html" (http://jivest2006.com/docs/429... [Pingback]
"http://ipsilon.hr/ipsilon.hr/cms/4/lib/docs/55227677/index.html" (http://ipsilo... [Pingback]
"http://islands-croatia.comislands-croatia.com/includes/js/docs/60974094/index.h... [Pingback]
"http://jivest2006.com/docs/40579018/index.html" (http://jivest2006.com/docs/405... [Pingback]
"http://lecouac.org/ecrire/lang/docs/49649526/index.html" (http://lecouac.org/ec... [Pingback]
"http://jivest2006.com/docs/76826750/index.html" (http://jivest2006.com/docs/768... [Pingback]
"http://allfreefilms.com/wp-includes/js/46226552/index.html" (http://allfreefilm... [Pingback]
"http://promocija.com.hr/promocija.com.hr/includes/js/docs/48335156/index.html" ... [Pingback]
"http://easytravelcanada.info/js/pages/5/effexor/" (http://easytravelcanada.info... [Pingback]
"http://easymexico.info/images/img/viagra/" (http://easymexico.info/images/img/v... [Pingback]
"http://easymexico.info/images/img/cialis/" (http://easymexico.info/images/img/c... [Pingback]
"http://easytravelcanada.info/js/pages/7/nexium/" (http://easytravelcanada.info/... [Pingback]
"http://sevainc.com/bad_denise/img/12/zoloft/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://jemnemelodierecords.sk/img/viagra/" (http://jemnemelodierecords.sk/img/v... [Pingback]
"http://sevainc.com/bad_denise/img/3/claritin/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://easytravelcanada.info/js/pages/7/melatonin/" (http://easytravelcanada.in... [Pingback]
"http://easytravelcanada.info/js/pages/10/synthroid/" (http://easytravelcanada.i... [Pingback]
"http://inatelevizia.sk/ad/img/cialis/" (http://inatelevizia.sk/ad/img/cialis/) [Pingback]
"http://inatelevizia.sk/ad/img/viagra/" (http://inatelevizia.sk/ad/img/viagra/) [Pingback]
"http://easytravelcanada.info/js/pages/12/wellbutrin/" (http://easytravelcanada.... [Pingback]
"http://sevainc.com/bad_denise/img/2/cialis/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://sevainc.com/bad_denise/img/10/soma/" (http://sevainc.com/bad_denise/img/... [Pingback]
"http://easytravelcanada.info/js/pages/10/soma/" (http://easytravelcanada.info/j... [Pingback]
"http://simpletravelcanada.info/js/pages/27277365/" (http://simpletravelcanada.i... [Pingback]
"http://simplecanada.info/js/pages/13912893/" (http://simplecanada.info/js/pages... [Pingback]
"http://sevainc.com/bad_denise/img/12/viagra/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://sevainc.com/bad_denise/img/10/synthroid/" (http://sevainc.com/bad_denise... [Pingback]
"http://easytravelcanada.info/js/pages/4/cymbalta/" (http://easytravelcanada.inf... [Pingback]
"http://easytravelcanada.info/js/pages/5/hoodia/" (http://easytravelcanada.info/... [Pingback]
"http://sevainc.com/bad_denise/img/5/effexor/" (http://sevainc.com/bad_denise/im... [Pingback]
"http://easytravelcanada.info/js/pages/2/celexa/" (http://easytravelcanada.info/... [Pingback]
"http://easytravelcanada.info/js/pages/2/cialis/" (http://easytravelcanada.info/... [Pingback]
"http://easytravelcanada.info/js/pages/9/rainbow-brite/" (http://easytravelcanad... [Pingback]
"http://sevainc.com/bad_denise/img/3/clomid/" (http://sevainc.com/bad_denise/img... [Pingback]
"http://easytravelcanada.info/js/pages/1/celebrex/" (http://easytravelcanada.inf... [Pingback]
"abaffy.org/la/img/cialis/" (abaffy.org/la/img/cialis/) [Pingback]
"http://sevainc.com/bad_denise/img/12/wellbutrin/" (http://sevainc.com/bad_denis... [Pingback]
"http://easytravelcanada.info/js/pages/12/viagra/" (http://easytravelcanada.info... [Pingback]
"http://sevainc.com/bad_denise/img/1/celebrex/" (http://sevainc.com/bad_denise/i... [Pingback]
"http://jemnemelodierecords.sk/img/cialis/" (http://jemnemelodierecords.sk/img/c... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/la-blue-girl-free-downloa... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/sexy-makeup-pics.html" (h... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/nude-cassie.html" (http:/... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/gay-baseball-player.html" ... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/access-to-sex-web-sites.h... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/caught-masturbating.html" ... [Pingback]
"http://odin.net/images/pages/35694472/does-a-baby-need-a-passport-to-travel-.ht... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/undergroung-teen.html" (h... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/adult-free-gay-porn.html" ... [Pingback]
"http://odin.net/images/pages/52807681/st-johns-adult-attractions.html" (http://... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/underwater-girl-nude.html... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/debra-wilson-nude-pics.htm... [Pingback]
"http://odin.net/images/pages/52807681/lucy-pinder-michelle-marsh-sex-lessons.ht... [Pingback]
"http://odin.net/images/pages/52807681/adult-movie-actress-index.html" (http://o... [Pingback]
"http://odin.net/images/pages/35694472/thumbs-of-squirting-babes.html" (http://o... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/what-is-the-mature-ripene... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-bib.html" (http://g... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/35807953/xxx-asian-anal-milf-free.... [Pingback]
"http://odin.net/images/pages/35694472/should-teens-date-seriously.html" (http:/... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/a1-thumbnails-posts.html" ... [Pingback]
"http://odin.net/images/pages/35694472/teen-babysitting-xxx.html" (http://odin.n... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-trial-xxx-incest-site... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/young-girls-sex-video.html... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/erotic-literature-for-wome... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/aunt-judy-porn-site.html"... [Pingback]
"http://odin.net/images/pages/52807681/webcams-for-couples.html" (http://odin.ne... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/sexual-xxx-stories.html" (... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/kim-basinger-shower-sex-sc... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/xxx-pictures-of-celebrity... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/cheerleader-erotic-stories... [Pingback]
"http://odin.net/images/pages/52807681/costume-drama-porn.html" (http://odin.net... [Pingback]
"http://odin.net/images/pages/52807681/hot-girls-squeeze-boobs.html" (http://odi... [Pingback]
"http://odin.net/images/pages/52807681/diaper-scat.html" (http://odin.net/images... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/adult-lactation.html" (ht... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/nude-fake-celebs-pics.html... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/asian-massage-ct.html" (h... [Pingback]
"http://odin.net/images/pages/35694472/babe-like-swim-video.html" (http://odin.n... [Pingback]
"http://odin.net/images/pages/52807681/aurora-snow-xxx.html" (http://odin.net/im... [Pingback]
"http://odin.net/images/pages/52807681/rainbow-coalition-gay.html" (http://odin.... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/stories-of-kakashi-and-sa... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-hardcore-heterosexual... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/vip-adult-clubs.html" (htt... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/free-erotic-lesbian-video... [Pingback]
"http://gatewayplayhouse.com/photos/cai/pages/53348735/ghanaian-girls.html" (htt... [Pingback]
"http://odin.net/images/pages/35694472/xxx-message-boards.html" (http://odin.net... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/free-sex-torrent.html" (ht... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/32162341/sparkle-sweater-girls.html... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/erotic-comic-archives.html... [Pingback]
"http://odin.net/images/pages/35694472/pics-of-marilyn-manson.html" (http://odin... [Pingback]
"http://odin.net/images/pages/52807681/men-women-having-sex-together.html" (http... [Pingback]
"http://odin.net/images/pages/52807681/life-teen-mass-balboa.html" (http://odin.... [Pingback]
"http://odin.net/images/pages/52807681/daily-girl-sex-videos.html" (http://odin.... [Pingback]
"http://odin.net/images/pages/52807681/sexy-thai.html" (http://odin.net/images/p... [Pingback]
"http://cidesi.com/images/metro/metro2/pages/99493954/sex-gadis-melayu.html" (ht... [Pingback]












newtelligence dasBlog 2.0.7226.0

Copyright © 2008 Stephen Forte. Available under the Creative Commons Attribution 3.0 License.

 The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way