Sum a Union Query
From: news.microsoft.com (noidea_at_nospam.com)
Date: 10/21/04
- Previous message: sam: "LoadLibrary failed on sqlsut.dll - sql server 2000 SP3"
- Next in thread: Hugo Kornelis: "Re: Sum a Union Query"
- Reply: Hugo Kornelis: "Re: Sum a Union Query"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 21 Oct 2004 15:55:50 +0100
Hi
I have data in 3 tables that I want to add together to get a total figure
So far I have an sp that uses a union query that gives me 3 totals (1 from
each table)
How can I now add these 3 totals together? is it a subquery of somesorts or
do I have to make a temp table of the union query results and then do a sum
on this?
Complete newbie so any help greatly appreciated.
Heres what I have so far:
CREATE PROCEDURE ems_OrderIntake
@DateFrom smalldatetime,
@DateTo smalldatetime
AS
SELECT TrnDate, SalesOrder, LineValue AS OrderValue, LineType
FROM dbo.SorAdditions
WHERE (TrnDate >=@DateFrom AND TrnDate <= @DateTo) AND (LineType = '1')
UNION
SELECT TrnDate, SalesOrder, - CancelledValue AS Cancels, LineType
FROM dbo.SorCancelled
WHERE (TrnDate >=@DateFrom AND TrnDate <= @DateTo) AND (LineType = '1')
UNION
SELECT TrnDate, SalesOrder, ChangeValue, LineType
FROM dbo.SorChanges
WHERE (TrnDate >=@DateFrom AND TrnDate <= @DateTo) AND (LineType = '1')
GO
- Previous message: sam: "LoadLibrary failed on sqlsut.dll - sql server 2000 SP3"
- Next in thread: Hugo Kornelis: "Re: Sum a Union Query"
- Reply: Hugo Kornelis: "Re: Sum a Union Query"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|