I am trying to sort a union query that is the basis of a cross tab. The query below is the combined query which is a summary query unioned to a presumarized query that is then crosstabbed. For some reason, I have been unable to get the query to sort in the order of Stu_Pop, Term, Status. I even tried breaking it down into several queries (though rather only use one since I have many different queries to do and would not want to have 3 to 6 queries for each) and could not get them to sort properly at their simplest level. I did one query, for 2009/10 and another for 2011 as select queries and then did a union together without the pivot and could not get the sort.
The historical data is already summed (linked in from Excel). The current data is at a detail level so is summed in the query itself (linked in from Oracle).
The first query is for 2011 data and the second query is for 2009 and 2010. When the query runs, the 2009 and 2010 data are together, but the 2011 data appears at the bottom. Here is an example of how it should look:
[tt]
StatusSort TERM STU_POP STATUS TOTAL DOM INTL
10 2009 Freshmen Applied
12 2009 Freshmen Accepted
14 2009 Freshmen Deposit
17 2009 Freshmen Enrolled
10 2010 Freshmen Applied
12 2010 Freshmen Accepted
14 2010 Freshmen Deposit
17 2010 Freshmen Enrolled
10 2011 Freshmen Applied
12 2011 Freshmen Accepted
14 2011 Freshmen Deposit
17 2011 Freshmen Enrolled
10 2009 Transfer Applied
12 2009 Transfer Accepted
14 2009 Transfer Deposit
17 2009 Transfer Enrolled
10 2010 Transfer Applied
12 2010 Transfer Accepted
14 2010 Transfer Deposit
17 2010 Transfer Enrolled
10 2011 Transfer Applied
12 2011 Transfer Accepted
14 2011 Transfer Deposit
17 2011 Transfer Enrolled
[/tt]
This is what is coming out
[tt]
StatusSort TERM STU_POP STATUS TOTAL DOM INTL
10 2009 Freshmen Applied
12 2009 Freshmen Accepted
14 2009 Freshmen Deposit
17 2009 Freshmen Enrolled
10 2010 Freshmen Applied
12 2010 Freshmen Accepted
14 2010 Freshmen Deposit
17 2010 Freshmen Enrolled
10 2009 Transfer Applied
12 2009 Transfer Accepted
14 2009 Transfer Deposit
17 2009 Transfer Enrolled
10 2010 Transfer Applied
12 2010 Transfer Accepted
14 2010 Transfer Deposit
17 2010 Transfer Enrolled
10 2011 Freshmen Applied
12 2011 Freshmen Accepted
14 2011 Freshmen Deposit
17 2011 Freshmen Enrolled
10 2011 Transfer Applied
12 2011 Transfer Accepted
14 2011 Transfer Deposit
17 2011 Transfer Enrolled
[/tt]
Here is the combined query
Here is the broken down version which still won't sort properly:
Query1 Current
Query2 Hist
Query3 Combined
I also tried union all and
which also failed to sort as I'd like
The historical data is already summed (linked in from Excel). The current data is at a detail level so is summed in the query itself (linked in from Oracle).
The first query is for 2011 data and the second query is for 2009 and 2010. When the query runs, the 2009 and 2010 data are together, but the 2011 data appears at the bottom. Here is an example of how it should look:
[tt]
StatusSort TERM STU_POP STATUS TOTAL DOM INTL
10 2009 Freshmen Applied
12 2009 Freshmen Accepted
14 2009 Freshmen Deposit
17 2009 Freshmen Enrolled
10 2010 Freshmen Applied
12 2010 Freshmen Accepted
14 2010 Freshmen Deposit
17 2010 Freshmen Enrolled
10 2011 Freshmen Applied
12 2011 Freshmen Accepted
14 2011 Freshmen Deposit
17 2011 Freshmen Enrolled
10 2009 Transfer Applied
12 2009 Transfer Accepted
14 2009 Transfer Deposit
17 2009 Transfer Enrolled
10 2010 Transfer Applied
12 2010 Transfer Accepted
14 2010 Transfer Deposit
17 2010 Transfer Enrolled
10 2011 Transfer Applied
12 2011 Transfer Accepted
14 2011 Transfer Deposit
17 2011 Transfer Enrolled
[/tt]
This is what is coming out
[tt]
StatusSort TERM STU_POP STATUS TOTAL DOM INTL
10 2009 Freshmen Applied
12 2009 Freshmen Accepted
14 2009 Freshmen Deposit
17 2009 Freshmen Enrolled
10 2010 Freshmen Applied
12 2010 Freshmen Accepted
14 2010 Freshmen Deposit
17 2010 Freshmen Enrolled
10 2009 Transfer Applied
12 2009 Transfer Accepted
14 2009 Transfer Deposit
17 2009 Transfer Enrolled
10 2010 Transfer Applied
12 2010 Transfer Accepted
14 2010 Transfer Deposit
17 2010 Transfer Enrolled
10 2011 Freshmen Applied
12 2011 Freshmen Accepted
14 2011 Freshmen Deposit
17 2011 Freshmen Enrolled
10 2011 Transfer Applied
12 2011 Transfer Accepted
14 2011 Transfer Deposit
17 2011 Transfer Enrolled
[/tt]
Here is the combined query
Code:
TRANSFORM Sum(TOT) AS SumOfTOT
SELECT StatusSort, TERM, STU_POP, STATUS, Sum(TOT) AS TOTAL
FROM (SELECT Left(qryBJAp.status,2) AS StatusSort, qryBJAp.TERM, qryBJAp.STU_POP, Mid(qryBJAp.STATUS,4) AS STATUS, IIf(qryBJAp.Residency="D","DOM","INTL") AS HeaderRow, Sum(qryBJAp.TOT) AS TOT
FROM qryBJAp
WHERE qryBJAp.STUDENT_LEVEL="UG"
AND TERM = "2011"
GROUP BY Left(qryBJAp.status,2), qryBJAp.TERM, qryBJAp.STU_POP, Mid(qryBJAp.STATUS,4), IIf(qryBJAp.Residency="D","DOM","INTL")
ORDER BY qryBJAp.STU_POP, qryBJAp.TERM
UNION
SELECT UnpivotData.StatusSort, UnpivotData.Term, UnpivotData.STU_POP, UnpivotData.STATUS, UnpivotData.HeaderRow, UnpivotData.SumTotal
FROM UnpivotData
WHERE UnpivotData.StatusSort In (10,12,14,17)
AND UnpivotData.HeaderRow<>"TOTAL"
AND UnpivotData.TabName = "UG_DOMInt"
AND TERM Between "2009" and "2010"
) AS [%$##@_Alias]
GROUP BY StatusSort, TERM, STU_POP, STATUS
ORDER BY STU_POP, TERM
PIVOT HeaderRow;
Here is the broken down version which still won't sort properly:
Query1 Current
Code:
SELECT Val(Left([qryBJAp].[status],2)) AS StatusSort, qryBJAp.TERM, qryBJAp.STU_POP, Mid(qryBJAp.STATUS,4) AS STATUS, IIf([qryBJAp].[Residency]="D","DOM","INTL") AS HeaderRow, Sum(qryBJAp.TOT) AS TOTAL
FROM qryBJAp
WHERE (((qryBJAp.STUDENT_LEVEL)="UG"))
GROUP BY Val(Left([qryBJAp].[status],2)), qryBJAp.TERM, qryBJAp.STU_POP, Mid(qryBJAp.STATUS,4), IIf([qryBJAp].[Residency]="D","DOM","INTL")
ORDER BY qryBJAp.STU_POP, qryBJAp.TERM;
Query2 Hist
Code:
SELECT UnpivotData.StatusSort, UnpivotData.Term, UnpivotData.STU_POP, UnpivotData.STATUS, UnpivotData.HeaderRow, Sum(UnpivotData.SumTotal) AS SumOfSumTotal
FROM UnpivotData
WHERE (((UnpivotData.StatusSort) In (10,12,14,17)) AND ((UnpivotData.HeaderRow)<>"TOTAL") AND ((UnpivotData.TabName)="UG_DOMInt") AND ((UnpivotData.TERM) Between "2009" And "2010"))
GROUP BY UnpivotData.StatusSort, UnpivotData.Term, UnpivotData.STU_POP, UnpivotData.STATUS, UnpivotData.HeaderRow
ORDER BY UnpivotData.STU_POP;
Code:
SELECT *
FROM qryUGDomInt_Hist
UNION
SELECT *
FROM qryUGDomInt_Current
ORDER BY STU_POP, TERM, STATUSSORT
I also tried union all and
Code:
SELECT *
FROM (
SELECT *
FROM qryUGDomInt_Hist
UNION ALL
SELECT *
FROM qryUGDomInt_Current
)
ORDER BY STU_POP, TERM, STATUSSORT
which also failed to sort as I'd like