1

我正在使用带有 c# 的 SQL Server 2005 和 asp.net 2008 ...我有 2 个表 Result 和 Stud_Info ....

1] Stud_Info

CREATE TABLE Stud_Info
(Enroll_Number varchar(20) NOT NULL,  
Salutation varchar(10) NULL,  
First_Name varchar(20) NULL,  
Middle_Name varchar(20) NULL,  
Last_Name varchar(20) NULL,  
Course_Id varchar(20) NULL,  
Batch varchar(20) NULL)

INSERT into Stud_Info values(11161,'Mr.','Mack','B','Botha','MECH','Batch1');    
INSERT into Stud_Info values(11162,'Mr.','John','A','Los','CIVIL','Batch2');    
INSERT into Stud_Info values(11163,'Ms.','Merry','F','Dsuza','ELCT','Batch1');    
INSERT into Stud_Info values(11164,'Mr.','Pow','B','Janero','MECH','Batch2');    
INSERT into Stud_Info values(11165,'Mr.','Martin','J','Smith','MECH','Batch1');    

SELECT * from Stud_Info  

第二张桌子是这样的...

2] Exam_Result

CREATE TABLE Exam_Result
(Result_Id numeric(18, 0) IDENTITY(1,1) NOT NULL,  
Enroll_Number varchar(50) NULL,  
Student_Name varchar(100) NULL,  
Course_Id varchar(50) NULL,  
Semester varchar(50) NULL,  
Subject_Id varchar(50) NULL,  
Subject_Name varchar(50) NULL,  
MarksObtained numeric(18, 0) NULL,  
Exam_Type varchar(50) NULL)

INSERT into Exam_Result values(11161,'Mack B Botha','MECH',1,'MT','Maths',25,'Internal1');  
INSERT into Exam_Result values(11161,'Mack B Botha','MECH',1,'EN','English',22,'Internal1');  
INSERT into Exam_Result values(11161,'Mack B Botha','MECH',1,'SC','Science',20,'Internal1');  
INSERT into Exam_Result values(11166,'Barden V John','CIVIL',1,'SS','Social',21,'Internal2');  
INSERT into Exam_Result values(11161,'Mack B Botha','MECH',2,'SM','Simple Maths',24,'Internal2');  
INSERT into Exam_Result values(11161,'Mack B Botha','MECH',2,'SM','Simple Maths',69,'Final');  

SELECT * from Exam_Result

我正在使用此存储过程将主题的行动态转换为列......并且它工作正常。

    Create Proc GetExamResults (@Course_Id varchar(100), @Semester varchar(10))
as
begin
    declare @subjname varchar(100)  
    declare @subjects varchar(7000)  
    declare @subjectsselection varchar(7000)  
    declare @SumSelection varchar(7000)  
    declare @NoOfSubjects int
    set @NoOfSubjects = 0

    set @subjects = ''  
    set @subjectsselection = '' 
    set @SumSelection = ''

    DECLARE subject_cursor CURSOR  
    FOR SELECT distinct Subject_Name FROM Exam_Result where course_id = @Course_Id And Semester = @Semester 

    OPEN subject_cursor  

    FETCH NEXT FROM subject_cursor  
    INTO @subjname  

    WHILE @@FETCH_STATUS = 0  
    BEGIN  
        set @subjects = @subjects + '[' + @subjname + '],'  
        set @subjectsselection = @subjectsselection + 'Sum(Isnull([' + @subjname + '],0)) As [' + @subjname + '],' 
        set @SumSelection = @SumSelection + 'Sum(Isnull([' + @subjname + '],0))+' 

        set @NoOfSubjects = @NoOfSubjects + 1

        FETCH NEXT FROM subject_cursor  
        INTO @subjname  
    End  
    CLOSE subject_cursor;  
    DEALLOCATE subject_cursor;  

    select @subjects = LEFT(@subjects, LEN(@subjects) - 1)  
    select @subjectsselection = LEFT(@subjectsselection, LEN(@subjectsselection) - 1)  
    select @SumSelection = LEFT(@SumSelection, LEN(@SumSelection) - 1)  

    print @subjects  
    print @subjectsselection  
    print @SumSelection

    declare @query nvarchar(4000)  

    set @query = 'select S.Enroll_Number, pvt.Student_Name, pvt.Course_Id, pvt.Semester, ' + @subjectsselection + ',' 
    set @query = @query + 'Exam_Type,' + @SumSelection + ' As Grand_Total, '
    set @query = @query + '(' + @SumSelection + ')' + '/' + convert(varchar(10),@NoOfSubjects) + ' As Avg'
    set @query = @query + ' From '  
    set @query = @query + '(select Enroll_Number, Student_Name, Course_Id, Semester, Subject_Name, MarksObtained, Exam_Type from Exam_Result ) ps '  
    set @query = @query + ' pivot(sum(MarksObtained) for Subject_Name in (' + @subjects + ')) as pvt'  
    set @query = @query + ' inner join Stud_Info S on S.Enroll_Number = pvt.Enroll_Number '
    set @query = @query + ' where pvt.Course_Id = ''' + @Course_Id + ''' and pvt.Semester = ''' + @Semester + ''''
    set @query = @query + ' group by S.Enroll_Number, pvt.Student_Name, pvt.Course_Id, pvt.Semester, Exam_Type'
    print @query
    exec sp_executesql @query  
end

目前我越来越喜欢....

Enroll_Number  Student_Name Course_Id   Semester  Maths    English    Science    Social    Smathas    total    avg

11161           MACK         MECH        1         25       22         20         0         0          67      total/all sub
11166           Barden       CIV         1         0        0          0          21        0          21      total/all sub

笔记

在这里,我得到了 Exam_Result 表中的所有科目以及所有子项的总和(在 ge 中,您可以看到 - 67)以及 Exam_Result 中所有科目的平均值

现在的问题是我想根据用户的选择通过摸索来显示结果......例如,如果用户只想看到Course_Id = MECH 和 Semester = 1 ....o/p 应该是....

Enroll_No    Student_Name    Course_ID    Semester    Maths    English    Science     Type         Grand_Total    Avg

11161        Mack B Botha     MECH         1          25        22         20        internal1       67          66.22

每门课程和学期都没有固定的科目..它可能会改变....并且需要按 Course_Id 和 Semester 分组给我指导和查询,以便我实施您的答案....我希望这些信息足以解释我的东西....所有答案都是最受欢迎的...谢谢

4

1 回答 1

1

回答你的这个评论:

它在我的站点的 sql server 2005 中不起作用它给出错误消息 102,级别 15,状态 1,第 1 行“ps”附近的语法不正确。传递给 SUBSTRING 函数的长度参数无效

第一个错误可能发生在执行动态查询时。我不能确定,但​​看起来您的数据库的兼容性级别低于 90,因此PIVOT不允许/识别。在此站点中搜索有关如何在没有 PIVOT 子句的情况下解决它的问题。或者,如果可能,将您的数据库的兼容性级别设置为 90。

至于第二个错误,我在测试你的查询时也遇到了。(当我没有为参数指定值时发生这种情况。)错误的来源是这一行:

select @subjects = LEFT(@subjects, LEN(@subjects) - 1)

如果游标查询不返回行,则您的@subjects变量保持为空并LEN(@subjects) - 1导致-1,当作为长度参数传递给 时LEFT(),会产生错误。

只需添加一个条件即可避免该问题:

if LEN(@subjects) > 0 select @subjects = LEFT(@subjects, LEN(@subjects) - 1)

对其他两个字符串 (@subjectsselection@SumSelection) 执行相同操作。

并且,请下次您提出问题并说某些东西不起作用时,请确保将所有相关信息(包括错误消息)放入您的问题中,而不是评论中。

于 2012-05-02T10:45:04.847 回答