这样个表结构,是一个成绩记录的表,id为自增标示,userid表示学生的id,classid表示课程的id,classvalues标示这个学生的这门课程的考试成绩打算一句sql出下面这样的结果 有没有哪位高手帮忙解决一下谢谢了。分数倾囊相送下面是脚本sqlUSE [test]
GO
/****** 对象:  Table [dbo].[Record]    脚本日期: 01/06/2009 10:22:58 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Record](
 [id] [int] IDENTITY(1,1) NOT NULL,
 [userId] [int] NULL,
 [classId] [int] NULL,
 [ClassValues] [int] NULL
) ON [PRIMARY]
USE [test]
GO
/****** 对象:  Table [dbo].[User]    脚本日期: 01/06/2009 10:23:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[User](
 [id] [int] NULL,
 [name] [nchar](10) NULL,
 [orderCode] [int] NULL
) ON [PRIMARY]
USE [test]
GO
/****** 对象:  Table [dbo].[Class]    脚本日期: 01/06/2009 10:23:25 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Class](
 [Id] [int] NULL,
 [className] [nchar](10) NULL
) ON [PRIMARY]提问者:汤包 - 初学一级
问题补充:用户名和课程名,比如语文,数学,政治都是单独存在其他表中的
要实现复合表头,(最好能自动出表头),和多个排名是必须的。而且前面的学生姓名是按照拼音或者其他一定规律定好的顺序,而且成绩排名是每个学科从高分到低分进行的排名。排名不能打乱学生姓名的排列顺序

解决方案 »

  1.   

    找dawugui去- -#媒婆給10分。。
      

  2.   


     正好跟我的这个一样。我可是花了 200 分啊,现在与你分享。 http://topic.csdn.net/u/20090102/10/4bd1a02a-0a0a-40a5-a8e6-a76d3784a0ba.html
      

  3.   

    去sql server區發貼吧
    高手如云
      

  4.   

    --建立测试环境
    set nocount on
    create table testRecord(id int,userid int,classid int,classvalue int)
    insert into testRecord select '1','2','1','80'
    insert into testRecord select '2','3','1','70'
    insert into testRecord select '3','4','1','60'
    insert into testRecord select '4','2','2','50'
    insert into testRecord select '5','3','2','40'
    insert into testRecord select '6','4','2','30'
    insert into testRecord select '7','2','3','20'
    insert into testRecord select '8','3','3','10'
    insert into testRecord select '9','4','3','90'
    go
    create table testUser(id int,name varchar(10))
    insert into testUser select 2,'张三'
    insert into testUser select 3,'李四'
    insert into testUser select 4,'王五'create table testClass(id int,classname varchar(10))
    insert into testClass select 1,'语文'
    insert into testClass select 2,'数学'
    insert into testClass select 3,'英语'
    --测试select name
    ,max(case when classid=1 then classvalue else 0 end)语文
    ,max(case when classid=1 then rank else 0 end)排名
    ,max(case when classid=2 then classvalue else 0 end)数学
    ,max(case when classid=2 then rank else 0 end)排名
    ,max(case when classid=3 then classvalue else 0 end)英语
    ,max(case when classid=3 then rank else 0 end)排名
     from(
    select *,rank=RANK()over(partition by classname order by classvalue desc) from(
    select a.userid,b.name,a.classid,c.classname,a.classvalue
     from testRecord a,testUser b,testClass c
    where a.userid=b.id and a.classid=c.id
    )a)b
    group by name,userid order by userid
    --删除测试环境
    drop table testRecord
    drop table testUser
    drop table testClass
     set nocount off/*
    name       语文          排名                   数学          排名                   英语          排名
    ---------- ----------- -------------------- ----------- -------------------- ----------- --------------------
    张三         80          1                    50          1                    20          2
    李四         70          2                    40          2                    10          3
    王五         60          3                    30          3                    90          1
    */