rank(),允许并列名次、复制名次自动空缺,结果如12245558……

本文详细介绍了SQL中的排名函数rank()的使用方法,包括如何按ID分组进行排名以及全局排名。通过具体示例展示了如何在students表中,针对每个ID进行局部排名,并给出全局分数排名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将score按ID分组排名:rank() over(partition by id order by score desc)

将score不分组排名:rank() over(order by score desc)

select id,area,score,

rank() over(partition by id order by score desc) 分组id排序,

rank() over(order by score desc) 不分组排序

from students order by id,area;

转载于:https://www.cnblogs.com/fanweisheng/p/11118958.html

在该代码基础上改为2.0版本:#include <iostream> #include <iomanip> #include <algorithm> #include <string> using namespace std; const int MAX_STUDENTS = 30; struct Student { string id; float score; }; // 录入学生信息 void inputStudents(Student students[], int &count) { cout << "请输入学生人数 (最多30人): "; cin >> count; if (count > MAX_STUDENTS || count <= 0) { cout << "人数无效,设置为最大值" << MAX_STUDENTS << endl; count = MAX_STUDENTS; } for (int i = 0; i < count; ++i) { cout << "\n第" << i + 1 << "个学生:\n"; cout << "学号: "; cin >> students[i].id; cout << "成绩: "; cin >> students[i].score; // 验证成绩范围 while (students[i].score < 0 || students[i].score > 100) { cout << " 无效成绩! 请输入0-100之间的值: "; cin >> students[i].score; } } cout << "录入完成!\n"; } // 计算总分和平均分 void calculateTotalAverage(Student students[], int count) { float total = 0; for (int i = 0; i < count; ++i) { total += students[i].score; } cout << "总分: " << fixed << setprecision(1) << total << endl; cout << "平均分: " << fixed << setprecision(2) << (total / count) << endl; } // 按成绩降序排序(高到低) void sortByScore(Student students[], int count) { sort(students, students + count, [](const Student &a, const Student &b) { return a.score > b.score; }); cout << "\n按成绩降序排名:\n"; cout << "排名\t学号\t成绩\n"; int rank = 1; for (int i = 0; i < count; ++i) { if (i > 0 && students[i].score != students[i-1].score) { rank = i + 1; } cout << rank << "\t" << students[i].id << "\t" << students[i].score << endl; } } // 按学号升序排序 void sortById(Student students[], int count) { sort(students, students + count, [](const Student &a, const Student &b) { return a.id < b.id; }); cout << "\n按学号升序排序:\n"; cout << "学号\t成绩\n"; for (int i = 0; i < count; ++i) { cout << students[i].id << "\t" << students[i].score << endl; } } // 按学号查询 void searchById(Student students[], int count) { string targetId; cout << "请输入查询学号: "; cin >> targetId; // 按成绩排序确定排名 Student temp[MAX_STUDENTS]; copy(students, students + count, temp); sort(temp, temp + count, [](const Student &a, const Student &b) { return a.score > b.score; }); int rank = -1; float score = -1; // 查找学生 for (int i = 0; i < count; ++i) { if (students[i].id == targetId) { score = students[i].score; // 在排序后数组中找排名 for (int j = 0; j < count; ++j) { if (temp[j].id == targetId) { rank = j + 1; // 处理并列情况 while (j > 0 && temp[j].score == temp[j-1].score) { rank--; j--; } break; } } break; } } if (rank != -1) { cout << "学号: " << targetId << endl; cout << "成绩: " << score << endl; cout << "排名: " << rank << endl; } else { cout << "未找到学号: " << targetId << endl; } } // 统计分数段 void analyzeGrades(Student students[], int count) { int levels[5] = {0}; // 不及格,及格,中等,良好,优秀 for (int i = 0; i < count; ++i) { if (students[i].score >= 90) levels[4]++; else if (students[i].score >= 80) levels[3]++; else if (students[i].score >= 70) levels[2]++; else if (students[i].score >= 60) levels[1]++; else levels[0]++; } string categories[] = {"不及格(0-59)", "及格(60-69)", "中等(70-79)", "良好(80-89)", "优秀(90-100)"}; cout << "\n成绩统计分析\n"; cout << "类别\t\t人数\t百分比\n"; for (int i = 0; i < 5; ++i) { float percentage = (static_cast<float>(levels[i]) / count) * 100; cout << categories[i] << "\t" << levels[i] << "\t" << fixed << setprecision(1) << percentage << "%\n"; } } // 显示所有学生记录 void displayAll(Student students[], int count) { cout << "\n学生记录:\n"; cout << "学号\t成绩\n"; for (int i = 0; i < count; ++i) { cout << students[i].id << "\t" << students[i].score << endl; } } int main() { Student students[MAX_STUDENTS]; int count = 0; int choice; do { cout << "\n学生成绩管理系统V1.0\n"; cout << "1.录入学生成绩\n"; cout << "2.计算总分和平均分\n"; cout << "3.按成绩降序排序\n"; cout << "4.按学号升序排序\n"; cout << "5.按学号查询\n"; cout << "6.分数段统计分析\n"; cout << "7.显示所有学生记录\n"; cout << "0. 退出\n"; cout << "请选择操作: "; cin >> choice; switch (choice) { case 1: inputStudents(students, count); break; case 2: if (count > 0) calculateTotalAverage(students, count); else cout << "请先录入学生数据!\n"; break; case 3: if (count > 0) sortByScore(students, count); else cout << "请先录入学生数据!\n"; break; case 4: if (count > 0) sortById(students, count); else cout << "请先录入学生数据!\n"; break; case 5: if (count > 0) searchById(students, count); else cout << "请先录入学生数据!\n"; break; case 6: if (count > 0) analyzeGrades(students, count); else cout << "请先录入学生数据!\n"; break; case 7: if (count > 0) displayAll(students, count); else cout << "请先录入学生数据!\n"; break; case 0: cout << "系统已退出!\n"; break; default: cout << "无效选择,请重新输入!\n"; } } while (choice != 0); return 0; }
最新发布
06-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值