在oracle中分组排名

时间:2015-07-23 09:02:38

标签: sql oracle

我有一个查询

Select age,qualification,sum(income) as total_income  from employee
group by age,qualification;

我想找到年龄和资格组的基于total_income的排名。

例如

19|Grad|5000|rank:1
19|Grad|4000|rank:2
19|Grad|3000|rank:3
26|Grad|6000|rank:1
26|Grad|5000|rank:2
26|PosG|8000|rank:1
26|PosG|6000|rank:2

我可以在Oracle中完成吗?我试过分区但不能弄明白。

3 个答案:

答案 0 :(得分:4)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE Employees ( Age, Qualification, Income ) AS
          SELECT 19, 'Grad', 5000 FROM DUAL
UNION ALL SELECT 19, 'Grad', 4000 FROM DUAL
UNION ALL SELECT 19, 'Grad', 3000 FROM DUAL
UNION ALL SELECT 26, 'Grad', 6000 FROM DUAL
UNION ALL SELECT 26, 'Grad', 5000 FROM DUAL
UNION ALL SELECT 26, 'PosG', 8000 FROM DUAL
UNION ALL SELECT 26, 'PosG', 6000 FROM DUAL;

查询1

SELECT Age,
       Qualification,
       Income,
       RANK() OVER ( PARTITION BY Age, Qualification ORDER BY Income DESC ) AS "Rank"
FROM   Employees

<强> Results

| AGE | QUALIFICATION | INCOME | Rank |
|-----|---------------|--------|------|
|  19 |          Grad |   5000 |    1 |
|  19 |          Grad |   4000 |    2 |
|  19 |          Grad |   3000 |    3 |
|  26 |          Grad |   6000 |    1 |
|  26 |          Grad |   5000 |    2 |
|  26 |          PosG |   8000 |    1 |
|  26 |          PosG |   6000 |    2 |

查询2

WITH total_incomes AS (
  SELECT Age,
         Qualification,
         SUM( Income ) AS total_income
  FROM   Employees
  GROUP BY
         Age,
         Qualification
)
SELECT Age,
       Qualification,
       total_income,
       RANK() OVER ( ORDER BY total_income DESC ) AS "Rank"
FROM   total_incomes

<强> Results

| AGE | QUALIFICATION | TOTAL_INCOME | Rank |
|-----|---------------|--------------|------|
|  26 |          PosG |        14000 |    1 |
|  19 |          Grad |        12000 |    2 |
|  26 |          Grad |        11000 |    3 |

答案 1 :(得分:0)

quiz.h

@property (nonatomic,strong) IBOutlet UITextView *textField;
@property (nonatomic, strong)  NSString *text;



quiz.m
- (IBAction)next:(id)sender {

NSString *text = self.textField.text;

if ([text length] == 0) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error!"
                                                            message:@"Enter some text"
                                                           delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];
    } else {
        selectFriendsViewController *sfvc = [[selectFriendsViewController alloc] init];
        sfvc.string = text;
    }

}



selectfriendsviewcontroller.h
@property (nonatomic, retain)  NSString *string;


selectfriendsviewcontroller.m

@synthezise string;
- (void)viewDidLoad {
[super viewDidLoad];

quizViewController *qvc = [[quizViewController alloc] init];
qvc.text = string;
UITextView *textfield = [[UITextView alloc] init];
string = textfield.text;
}

答案 2 :(得分:0)

Select 
          Age,
          Qualification,
          sum(income) as totalIncome,
          dense_rank () over (order by 
                             sum(income) desc) as DRnk    from exam_RG 
    group by age,Qualification

Age Qualification   totalIncome DRnk
26  Grad            21000        1
19  Grad            12000        2