培训考试项目中,需要实现考试成绩排名:排名参考项为分数(score降序)、参加日期(attendtime升序)、第几次参加考试(frequency升序);并且,每个用户只保留一条数据(pid)。

考试结果存储表格如下:

期望得到的结果为:

解决思路:

  • 去重:

    • 考虑到dintinct针对单个字段比较有效,结合其他字段使用时,效果不理想;
    • 嵌套语句先进行排名,再去除重复的pid数据行;尝试半天没写出来;请教同学,由他给出下一条方案
    • 使用临时表,分语句查询;先排名为temp1表,后在temp1表中删除分数小的重复的pid行,再删除考试次数大的重复的pid行,添加rankid字段结合其他字段生成新的temp2临时表;此方法经测试可行,创建为存储过程代码如下:
 Create  PROCEDURE [dbo].[myZhenxin]
@examid int
AS
BEGIN
if object_id('tempdb..#temp1') is not null
Begin
drop table #temp1
End if object_id('tempdb..#temp2') is not null
Begin
drop table #temp2
End -- Insert statements for procedure here
select id,frequency,attendtime, examid,score,pid into #temp1 from ExamResult where examid=@examid order by score desc,attendtime delete #temp1 where id in(select a.id from #temp1 a, #temp1 b where a.pid = b.pid and a.score<b.score) delete #temp1 where id in(select a.id from #temp1 a, #temp1 b where a.pid = b.pid and a.frequency>b.frequency ) select IDENTITY(int,,) rankid, examid,pid,score,frequency,attendtime into #temp2 from #temp1 select * from #temp2
END

在sql server中,调用存储过程的语句为:

exec myZhenxin ''

在php中使用sqlsrv调用存储过程:

 if ( ($stmt = sqlsrv_query($conn, $tsql)) )
{
// now, iterate through all the statements in
// stored proc or script $tsql:
do
{
// process the result of the iteration
if ( sqlsrv_num_fields($stmt) > 0 )
{
// we have a result set
while ( ($row=sqlsrv_fetch_array($stmt)) )
{
// do something with $row
}
}
else
{
// we have something else
$rowsAffected = sqlsrv_rows_affected($stmt);
}
} while ( ($next = sqlsrv_next_result($stmt)) ) ; if ( $next === NULL )
{
// it worked
}
else
{
// it didn't work, check sqlsrv_errors()
} sqlsrv_free_stmt($stmt);
}

最新文章

  1. AES加密算法C++实现
  2. jackson中JSON字符串节点遍历和修改
  3. BZOJ3825 : [Usaco2014 Nov]Marathon
  4. Cache模拟器(CacheSim)
  5. [置顶] 修改Android开机画面之rle制作
  6. 双击GridView查看详情
  7. 键盘皇者 RealForce 104Pro独家评测
  8. 如何获取Azure Storage Blob的MD5值
  9. 反射(4)反射性能问题:直接调用vs反射调用
  10. Java的this关键字在继承时的作用
  11. 【C#】解析C#中管道流的使用
  12. cocos2d-x 在输入文字时点击语音crash
  13. Git-忽略规则.gitignore生效
  14. springboot(五):spring data jpa的使用
  15. CSV文件插入到mysql表中指定列
  16. macOS &amp; SVN
  17. jsp指令和重定向
  18. nth-child,nth-last-child,only-child,nth-of-type,nth-last-of-type,only-of-type,first-of-type,last-of-type,first-child,last-child伪类区别和用法
  19. Spring学习笔记--使用注解装配
  20. 阅读《名师讲坛--Android开发实战经典》

热门文章

  1. django使用model创建数据库表使用的字段
  2. 基于angularJs的单页面应用seo优化及可抓取方案原理分析
  3. eclipse debug URLClassPath.getLoader(int) file
  4. redhat初始化yum源,使用阿里云yum源
  5. crontab问题处理
  6. docker--数据卷与数据卷容器
  7. 我的学习之路_第二十一章_JDBC连接池
  8. js禁止选中(网页复制)
  9. 国内互联网公司github网址
  10. 4.jsp的内置对象