Presentation is loading. Please wait.

Presentation is loading. Please wait.

Rank Order Function Farrokh Alemi, Ph.D.

Similar presentations


Presentation on theme: "Rank Order Function Farrokh Alemi, Ph.D."— Presentation transcript:

1 Rank Order Function Farrokh Alemi, Ph.D.
In this section we discuss the rank order function using SQL. This brief presentation was organized by Dr. Alemi.

2 Order Data Based on a Column Values
Cross Join Order Data Based on a Column Values Repeated Same Dx for Same Patient Rank The purpose of Rank and Rank Dense functions are to order the records based on the values of one column. For example, we can find out if a patient has been repeatedly admitted to the hospital for the same diagnosis, a situation that happens when the earlier treatment has not worked and the patient is readmitted for further treatment.

3 Rank Skips Rank Dense Does Not Cross Join Rank & Rank_Dense
Rank and Rank dense function differ in how they treat records where two or more records have the same value. If two records have the same rank, Rank function skips the next rank number. Rank dense does not.

4 Rank Skips: 1, 1, 3, 4 Rank Dense Does Not Cross Join
Rank & Rank_Dense For example, if two records are ranked to occur at the same order, at rank 1, then the rank function will assign rank 1 to both of them and rank 3 to the next record. It skips rank 2.

5 Rank Skips: 1, 1, 3, 4 Rank Dense Does Not: 1, 1, 2, 3 Cross Join
Rank & Rank_Dense In contrast, the rank dense will rank the first two at 1 and start the next one at 2.

6 Cross Join ID icd9 Age Rank 10 I 10 I 10 I 10 I 10 I 10 I Skip 4 Here we see an example of what is happening to patient number 10. He has the diagnosis 276.1, Hyposmolality, repeatedly at different ages. Two of these diagnoses are reported for the same age. Hence we see them ranked the same. We see Rank 1, then rank 2, next 2 rank 3, and rank 4 is missing and we jump to rank 5. This is an example of skip in the ranks.

7 If it makes sense, delete repeated entries
Advice: If it makes sense, delete repeated entries Cross Join Rank Skips: 1, 1, 3, 4 Rank Dense Does Not: 1, 1, 2, 3 Rank & Rank_Dense There is, of course, no difference between rank and rank dense, if no two records have the same order. This can be guaranteed by grouping by the fields used to order the rank, a first step that often should be done before using rank functions. When no two records have the same order then no two have same rank. Then we never have to deal with skip patterns and thus rank and rank dense functions produce same results.

8 Patient having same diagnosis at same time
Advice: Patient having same diagnosis at same time Cross Join Rank Skips: 3, 3, 5, 6 Rank Dense Does Not: 3, 3, 4, 5 Rank & Rank_Dense For example, patients may have same diagnosis on same hospital admission. Once they are seen by one doctor and another time by another clinician. To rank these diagnoses as two different times of having the same diagnosis is a mistake. In this situation, it makes sense to delete the repetitions of the diagnosis for the same person at the same time. This helps make the ranking task more efficient and more sensible.

9 Cross Join RANK ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> ) Rank Function Syntax Here is the syntax for the rank function.

10 Cross Join RANK ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> ) Rank Function Syntax The reserved word Rank starts the syntax. Since this function has no arguments and is followed with parentheses with nothing in them.

11 Cross Join RANK ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> ) Rank Function Syntax The syntax requires specification of the over clause, which requires us to specify what field or fields should be used to order the ranks.

12 Cross Join RANK ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> ) Rank Function Syntax The partition clause is optional and describes whether the rank order should restart in subgroups of the records.

13 Cross Join RANK ( ) OVER ( [ <partition_by_clause> ] <order_by_clause> ) Rank Function Syntax The partition clause is optional and describes whether the rank order should restart in subgroups of the records.

14 , Rank() OVER (partition by id, icd9 order by icd9, ageatdx)
Cross Join , Rank() OVER (partition by id, icd9 order by icd9, ageatdx) Rank Function Syntax Here is an example of the Rank command.

15 , Rank() OVER (partition by id, icd9 order by icd9, ageatdx)
Cross Join , Rank() OVER (partition by id, icd9 order by icd9, ageatdx) Rank Function Syntax The order by command says that we want to set the order based on type of ICD code and age at which it occurs.

16 , Rank() OVER (partition by id, icd9 order by icd9, ageatdx)
Cross Join , Rank() OVER (partition by id, icd9 order by icd9, ageatdx) Rank Function Syntax The partition command says that we want to organize the rank orders to start from 1 for each individual and each diagnosis.

17 Use Database called AgeDx
DROP TABLE #Temp USE AgeDx SELECT ID, icd9, AgeAtDx , Rank() OVER (partition by id, icd9 order by icd9, AgeAtDx) AS [Repeated Dx] INTO #Temp FROM dbo.final WHERE ID=10 GROUP BY ID, icd9, AgeAtDx Select * FROM #Temp ORDER BY ID, icd9, [Repeated Dx] Cross Join Use table called Final Rank Function Syntax Note that the code uses table final from database called AgeDx, these may have different names in your data.

18 For ease we are working with person with ID 10
DROP TABLE #Temp USE AgeDx SELECT ID, icd9, AgeAtDx , Rank() OVER (partition by id, icd9 order by icd9, AgeAtDx) AS [Repeated Dx] INTO #Temp FROM dbo.final WHERE ID=10 GROUP BY ID, icd9, AgeAtDx Select * FROM #Temp ORDER BY ID, icd9, [Repeated Dx] Cross Join For ease we are working with person with ID 10 Rank Function Syntax For ease we are working with person with ID 10. Otherwise, these steps will take a long time to carry out.

19 Duplicates are removed so rank and rank dense are same
DROP TABLE #Temp USE AgeDx SELECT ID, icd9, AgeAtDx , Rank() OVER (partition by id, icd9 order by icd9, AgeAtDx) AS [Repeated Dx] INTO #Temp FROM dbo.final WHERE ID=10 GROUP BY ID, icd9, AgeAtDx Select * FROM #Temp ORDER BY ID, icd9, [Repeated Dx] Cross Join Duplicates are removed so rank and rank dense are same Rank Function Syntax Duplicates are removed so rank and rank dense are the same. The group by command will delete any record for the same patient having more than 1 same diagnosis occurring at same age.

20 , Rank() OVER (partition by id, icd9
DROP TABLE #Temp USE AgeDx SELECT ID, icd9, AgeAtDx , Rank() OVER (partition by id, icd9 order by icd9, AgeAtDx) AS [Repeated Dx] INTO #Temp FROM dbo.final WHERE ID=10 GROUP BY ID, icd9, AgeAtDx Select * FROM #Temp ORDER BY ID, icd9, [Repeated Dx] Cross Join Order by Clause Rank Function Syntax The order by clause includes two variables

21 , Rank() OVER (partition by id, icd9
Partition by clause DROP TABLE #Temp USE AgeDx SELECT ID, icd9, AgeAtDx , Rank() OVER (partition by id, icd9 order by icd9, AgeAtDx) AS [Repeated Dx] INTO #Temp FROM dbo.final WHERE ID=10 GROUP BY ID, icd9, AgeAtDx Select * FROM #Temp ORDER BY ID, icd9, [Repeated Dx] Cross Join Rank Function Syntax The partition by clause also includes two variables.

22 ID icd9 AgeAtDx Repeated Dx 10 I041.89 64.666666 1 10 I112.0 64.25 1
Cross Join Rank Function Syntax The top 10 lines of the result of the query are provided here. All records refer to patient 10.

23 ID icd9 AgeAtDx Repeated Dx 10 I041.89 64.666666 1 10 I112.0 64.25 1
Cross Join Rank Function Syntax At 64.66, the patient was hospitalized with diagnosis , which is an unspecified bacterial infection. This infection did not repeat, so the rank does not exceed 1.

24 ID icd9 AgeAtDx Repeated Dx 10 I041.89 64.666666 1 10 I112.0 64.25 1
Cross Join Rank Function Syntax The next disease is 112.0, which is Candidiasis of mouth. This disease also does not repeat either.

25 ID icd9 AgeAtDx Repeated Dx 10 I041.89 64.666666 1 10 I112.0 64.25 1
Cross Join Rank Function Syntax The situation is different for hospitalization with diagnosis 253.6, which is “Other disorders of neurohypophysis.” The patient was hospitalized for this disease 3 times, first at 64.25, then at and later at years. We see that this disease is ranked 1, 2 and 3 for repetition.

26 ID icd9 AgeAtDx Repeated Dx 10 I041.89 64.666666 1 10 I112.0 64.25 1
Cross Join Rank Function Syntax Repetition also occurs for disease 272.4, which is unspecified hyperlipidemia.

27 Rank Order Function is useful in analysis of data in electronic health records
This section described the rank order function. This function is useful in analysis of data in electronic health records.


Download ppt "Rank Order Function Farrokh Alemi, Ph.D."

Similar presentations


Ads by Google