sqlÖеÄrank()ÔõôÓÃ
ºË?ÃÕµ×£ºsql ÖÐµÄ rank() º¯ÊýÓÃÓÚ·µ»ØÖ¸¶¨ÐÐÔÚЧ¹û¼¯ÖеÄÅÅÃû£¬»ùÓÚÐÐÖÐÖµµÄÅÅÐò¡£ÏêϸÐÎò£ºrank() º¯Êýͨ¹ý over ×Ó¾äÖ¸¶¨·ÖÇøºÍÅÅÐò±í´ïʽ¡£Ëüƾָ֤¶¨Áлò±í´ïʽµÄÅÅÐò¶ÔЧ¹û¼¯ÖеÄÐоÙÐÐÅÅÃû¡£ÏàֵͬµÄÅÅÃûÏàͬ£¬´Ó 1 ×îÏÈ¡£rank() º¯ÊýÔÚÿ¸ö·ÖÇøÄÚ×ÔÁ¦ÅÌËãÅÅÃû£¬ÕâÒâζ×Ųî±ð·ÖÇøÖоßÓÐÏàֵͬµÄÐеÄÅÅÃû¿ÉÄܲî±ð¡£
SQL ÖÐ RANK() º¯Êý
RANK() º¯Êý¼ò½é
RANK() º¯Êý·µ»ØÖ¸¶¨ÐÐÔÚЧ¹û¼¯ÖеÄÅÅÃû¡£ÅÅÃû»ùÓÚÐÐÖÐÖµµÄÅÅÐò£¬²¢ÇÒÏàֵͬµÄÅÅÃûÏàͬ¡£
Óï·¨
RANK() OVER (PARTITION BY partition_expression ORDER BY order_expression)
µÇ¼ºó¸´ÖÆ
²ÎÊý
partition_expression£ºÖ¸¶¨Òª¾ÙÐзÖÇøµÄÁлò±í´ïʽ£¬ÓÃÓÚ½«Ð§¹û¼¯·Ö×é¡£
order_expression£ºÖ¸¶¨ÓÃÓÚ¶Ô·ÖÇø¾ÙÐÐÅÅÐòµÄÁлò±í´ïʽ¡£
Ó÷¨
RANK() º¯Êýͨ³£ÓÃÓÚ¶ÔÅÌÎÊЧ¹û¾ÙÐÐÅÅÃû£¬ÀýÈ磺
ƾ֤ÏúÊÛ¶î¶Ô¿Í»§ÅÅÃû
ƾ֤ÈÕÆÚ¶ÔÊÂÎñÅÅÃû
ƾ֤Ч¹û¶ÔѧÉúÅÅÃû
ʾÀý
ÒÔÏÂʾÀý½«°´²¿·Ö¶ÔÔ±¹¤¾ÙÐÐÅÅÃû£¬²¢ÏÔʾÿ¸öÔ±¹¤µÄÅÅÃû£º
SELECT department, name, RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS employee_rank FROM employees;
µÇ¼ºó¸´ÖÆ
Ч¹û
department | name | employee_rank |
---|---|---|
Sales | John Doe | 1 |
Sales | Jane Smith | 2 |
Marketing | Michael Jones | 1 |
Marketing | Emily Carter | 2 |
Finance | David Brown | 1 |
Finance | Mary Miller | 2 |
×¢ÖØÊÂÏî
RANK() º¯Êý·µ»ØµÄÅÅÃû´Ó 1 ×îÏÈ¡£
ÈôÊÇÁ½¸ö»ò¸ü¶àÐоßÓÐÏàͬµÄÅÅÐòÖµ£¬ËüÃǽ«»ñµÃÏàͬµÄÅÅÃû¡£
RANK() º¯ÊýÔÚÿ¸ö·ÖÇøÄÚ×ÔÁ¦ÅÌËãÅÅÃû£¬ÕâÒâζ×ÅÔÚ²î±ðµÄ·ÖÇøÖоßÓÐÏàֵͬµÄÐеÄÅÅÃû¿ÉÄܲî±ð¡£
ÒÔÉϾÍÊÇsqlÖеÄrank()ÔõôÓõÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡