質問

I am using the same table and columns for both queries, but I have different aliases and avg function. How would I be able to combine these two queries with UNION ALL because I want every single row that I have selected from them. Obviously I can't do it when I have different select statements. How could I combine these two queries ?

SELECT GRADE_TYPE_CODE, ROUND(TO_CHAR(NUMERIC_GRADE),2) AS GRADE
FROM GRADE
WHERE STUDENT_ID = 10 AND SECTION_ID = 5
ORDER BY GRADE_TYPE_CODE DESC,NUMERIC_GRADE DESC;



SELECT 'Average is:' AS GRADE_TYPE_CODE,AVG(GRADE) AS GRADE
FROM
(
    SELECT AVG(NUMERIC_GRADE) AS GRADE
FROM GRADE
WHERE STUDENT_ID = 10 AND SECTION_ID = 5
GROUP BY GRADE_TYPE_CODE
) myInnerTable;
役に立ちましたか?

解決

Not sure what the issue is here. Why not just put "union all" between your two queries, and move the ORDER BY clause to the end?

SELECT GRADE_TYPE_CODE, ROUND(TO_CHAR(NUMERIC_GRADE),2) AS GRADE
FROM GRADE
WHERE STUDENT_ID = 10 AND SECTION_ID = 5

union all

SELECT 'Average is:' AS GRADE_TYPE_CODE,AVG(GRADE) AS GRADE
FROM
(
    SELECT AVG(NUMERIC_GRADE) AS GRADE
FROM GRADE
WHERE STUDENT_ID = 10 AND SECTION_ID = 5
GROUP BY GRADE_TYPE_CODE
) myInnerTable

order by 1 desc, 2 desc

http://sqlfiddle.com/#!4/99a16/19/0

他のヒント

I'm thinking you could create a temporary table and then union ALL from the two separate queries.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top