en-UShe-IL
You are here:   Blog
Register   |  Login

Blog Archive:

Maximize
* Can be used in order to search for older blogs Entries

Search in blogs


Blog Categories:

Maximize
* Can be used in order to search for blogs Entries by Categories

Blog Tags:

Maximize
* Can be used in order to search for blogs by keywords

TNWikiSummit


Awared MVP

 


Microsoft® Community Contributor 


Microsoft® Community Contributor


 Read this before you use the blog! Maximize

Recent Entries

Minimize
יול23

Written by: ronen ariely
23/07/2011 08:26 RssIcon

 

RANK query Without using RANK

כל שרתי הנתונים היום מאפשרים ליצור עמודת דירוג (RANK) בצורה קלה ומהירה. השיטה לייצר עמודה כזו שונה משרת אחד לשני. בהמשך אולי תציג דוגמאות כיצד ניתן לבצע זאת בכל אחד מהשרתים הנפוצים. עם זה לא חסר מאמרים כאלה ברשת האינטרנט.

מה שנראה כאן היא שאילתה תקנית בסיסית האמורה לעבוד על שרת נתונים וכל בסיס נתונים (בפרט בסיסי נתונים כקובצי טקסט, אקסל וכל שרתי הנתונים בכלל) בצורה פשוטה ויחסית מהירה.

שלבי עבודה:

נייצר טבלה לצרכי ההדגמה שלנו

create table ArielyRankWithOutRank([name] varchar(10))

GO

 

נכניס כמה נתונים לטבלה

insert ArielyRankWithOutRank

select 'A' union

select 'D' union

select 'V' union

select 'B' union

select 'N' union

select 'T' union

select 'H'

insert ArielyRankWithOutRank

select 'A' union

select 'D' union

select 'V' union

select 'B' union

select 'N' union

select 'T' union

select 'H'

insert ArielyRankWithOutRank

select 'fA' union

select 'gD' union

select 'hV' union

select 'jB' union

select 'kN' union

select 'lT' union

select 'yH'

GO

נבדקו את הנתונים בטבלה

select * from ArielyRankWithOutRank

הגיע הזמן להוצאת נתוני הדירוג שלנו

-------------------------------------------------- Rank Using RANK

 select *

 from (

        select RANK() over (Order by InTbl.[name]) RN,*

        from ArielyRankWithOutRank as InTbl

        ) as Tbl

 order by Tbl.[name]

 

 ----------------------------------------- RANK Without using RANK

 select (

        select COUNT(*)+1

        from ArielyRankWithOutRank as InTbl

        where InTbl.[name] < OutTbl.[name]

        ) as RN,*

 from ArielyRankWithOutRank OutTbl

 order by OutTbl.[name]