最初我正在处理两个查询, 一个人回来了学生的分数, 第二个是返回Grand Total Marks。
现在我决定联合这两个查询,但我的Grand Total行出现在顶部,我想在底部,我尝试更改两个的排序,但对结果没有影响。请提出建议,我的疑问是:
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')
答案 0 :(得分:2)
如果没有显式 ORDER BY
,查询结果的排序是任意的。
现在你可能会注意到总是的排序似乎是相同的,但是要保证:添加数据或更改聚簇索引或者什么会使你认为理所当然的订单无效。
通常,您可以通过
解决此问题示例强>
SELECT 1 AS ToSortOn, ... FROM ...
UNION ALL SELECT 2 AS ToSortOn, ... FROM ...
ORDER BY ToSortOn
答案 1 :(得分:1)
虽然不是最好的方法......
UNION ALL
答案 2 :(得分: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