1

Initially I was working with two query, One was returning Student's marks, second was returning Grand Total Marks.

Now I decided to Union these two queries but my Grand Total row is appearing at top, which I want at bottom, I tried changing the sequencing of two, but no impact on result. Please suggest, my query is:

SELECT AISECTRESULT_Schemes.SUBNO, 
        AISECTRESULT_Schemes.SUBJECT, 
        AISECTRESULT_Schemes.CE_TotalMarks, 
        AISECTRESULT_ExamMarksData.TotalMarksObt_C, 
        AISECTRESULT_Schemes.TE_TotalMarks, 
        AISECTRESULT_ExamMarksData.TotalMarksObt,
        MAXMARKS, 
        AISECTRESULT_ExamMarksData.OverAllMarks 
  FROM AISECTRESULT_Schemes 
 INNER JOIN AISECTRESULT_ExamMarksData 
    ON AISECTRESULT_Schemes.[EXAMSCHEME ID] = AISECTRESULT_ExamMarksData.EXAMSCHEMEID
 WHERE (AISECTRESULT_ExamMarksData.REGISTRATIONID = '201192145')

UNION

SELECT ''AS SUBNO,
       ''AS SUBJECT,
        SUM(AISECTRESULT_Schemes.CE_TotalMarks)CE_TotalMarks, 
        SUM(AISECTRESULT_ExamMarksData.TotalMarksObt_C)TotalMarksObt_C,
        SUM(AISECTRESULT_Schemes.TE_TotalMarks)TE_TotalMarks, 
        SUM(AISECTRESULT_ExamMarksData.TotalMarksObt)TotalMarksObt,
        SUM(AISECTRESULT_Schemes.MAXMARKS)MAXMARKS,
        SUM(AISECTRESULT_ExamMarksData.OverAllMarks)OverAllMarks
  FROM AISECTRESULT_Schemes
 INNER JOIN AISECTRESULT_ExamMarksData
    ON AISECTRESULT_Schemes.[EXAMSCHEME ID] = AISECTRESULT_ExamMarksData.EXAMSCHEMEID
 WHERE (AISECTRESULT_ExamMarksData.REGISTRATIONID = '201192145')

enter image description here

4

3 回答 3

2

查询结果的顺序是任意的,没有明确 ORDER BY的.

现在您可能会注意到排序似乎总是相同的,但请放心:添加数据或更改聚集索引或诸如此类会使您认为理所当然的顺序无效。

通常,您可以通过以下方式解决此问题

  • 添加一个虚拟列进行排序(如果没有合理的现有列可供选择)
  • 在该列上显式排序

例子

SELECT 1 AS ToSortOn, ... FROM ...
UNION ALL SELECT 2 AS ToSortOn, ... FROM ...
ORDER BY ToSortOn
于 2012-07-31T14:08:15.823 回答
1

虽然不是最好的方法...

UNION ALL
于 2012-07-31T14:08:16.350 回答
1

你需要一个ORDER BY关于你的查询。这应该这样做:

SELECT  SUBNO, 
        SUBJECT, 
        CE_TotalMarks, 
        TotalMarksObt_C, 
        TE_TotalMarks, 
        TotalMarksObt,
        MAXMARKS, 
        OverAllMark
FROM (  SELECT  AISECTRESULT_Schemes.SUBNO, 
                AISECTRESULT_Schemes.SUBJECT, 
                AISECTRESULT_Schemes.CE_TotalMarks, 
                AISECTRESULT_ExamMarksData.TotalMarksObt_C, 
                AISECTRESULT_Schemes.TE_TotalMarks, 
                AISECTRESULT_ExamMarksData.TotalMarksObt,
                MAXMARKS, 
                AISECTRESULT_ExamMarksData.OverAllMarks,
                1 ColOrder
        FROM AISECTRESULT_Schemes 
        INNER JOIN AISECTRESULT_ExamMarksData 
        ON AISECTRESULT_Schemes.[EXAMSCHEME ID] = AISECTRESULT_ExamMarksData.EXAMSCHEMEID
        WHERE AISECTRESULT_ExamMarksData.REGISTRATIONID = '201192145'

        UNION

        SELECT  ''AS SUBNO,''AS SUBJECT,
                SUM(AISECTRESULT_Schemes.CE_TotalMarks)CE_TotalMarks, 
                SUM(AISECTRESULT_ExamMarksData.TotalMarksObt_C)TotalMarksObt_C,
                SUM(AISECTRESULT_Schemes.TE_TotalMarks)TE_TotalMarks, 
                SUM(AISECTRESULT_ExamMarksData.TotalMarksObt)TotalMarksObt,
                SUM(AISECTRESULT_Schemes.MAXMARKS)MAXMARKS,
                SUM(AISECTRESULT_Exa![enter image description here][1]mMarksData.OverAllMarks)OverAllMarks,
                2
        FROM AISECTRESULT_Schemes 
        INNER JOIN AISECTRESULT_ExamMarksData 
        ON AISECTRESULT_Schemes.[EXAMSCHEME ID] = AISECTRESULT_ExamMarksData.EXAMSCHEMEID 
        WHERE AISECTRESULT_ExamMarksData.REGISTRATIONID = '201192145') AS Data
ORDER BY ColOrder
于 2012-07-31T14:08:54.890 回答