Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Aggregation Across a Union Query.

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2000-10-31 : 10:42:44
John writes "Is it possible to do an aggregation across a union query? The actual problem I am trying to work through with this approach is a little complicated to server for academic purposes, however, let me give as an example a pretty straightforward problem.

Assuming I have a table Orders that contains a 'productCode' and 'units' column to indicate the item and quantity of a product sold. To return the total number of each item sold, I could pefrorm a simple aggregation query like so:

SELECT ProductCode, Sum(Units)
FROM Orders
GROUP BY ProductCode


Now, for the purposes of this example, lets assume that we used horizontal partitioning techniques, and had an archive orders table where orders were moved after they grew older than a year. If you wanted to do the above query across both tables you would need to be able to do something similar to:

SELECT ProductCode, Sum(Units)
From Orders
UNION
SELECT ProductCode, Sum(Units)
From ArchiveOrders

GROUP By ProductCode

Where the last group by applied to both sides of the union query. Is this possible?"
   

- Advertisement -