Re: Sum a Union Query
From: Hugo Kornelis (hugo_at_pe_NO_rFact.in_SPAM_fo)
Date: 10/21/04
- Next message: JIM.H.: "& in the where clause"
- Previous message: news.microsoft.com: "Sum a Union Query"
- In reply to: news.microsoft.com: "Sum a Union Query"
- Next in thread: Newbie: "Re: Sum a Union Query"
- Reply: Newbie: "Re: Sum a Union Query"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 21 Oct 2004 21:27:11 +0200
On Thu, 21 Oct 2004 15:55:50 +0100, news.microsoft.com wrote:
>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
Hi,
I'm not sure what you mean. You say that your current query gives you
three totals, but the code you post doesn't include any SUM function.
I think I can help you better if you post more detailed information about
your problem. Try it with:
1. The table structure, posted as CREATE TABLE statements. Irrelevant
columns may be omitted, but please do include all defaults, keys and other
constraints. See http://www.aspfaq.com/etiquette.asp?id=5006.
2. Some sample data, in the form of INSERT statements. I don't need
hundreds of rows, just a sample that's large enough to illustrate your
problem. See http://vyaskn.tripod.com/code.htm#inserts.
Do test your CREATE TABLE and INSERT statements against an empty database
before posting them here and fix any errors they might contain.
3. The expected output, as based on the sample data you posted.
4. The query that you have so far (but you don't have to repost it if it's
the same as you posted before), the output this gives you (if an error:
copy and paste the complete error message) and why this is not what you
expected.
5. A concise description of the business problem you're trying to solve.
Best, Hugo
-- (Remove _NO_ and _SPAM_ to get my e-mail address)
- Next message: JIM.H.: "& in the where clause"
- Previous message: news.microsoft.com: "Sum a Union Query"
- In reply to: news.microsoft.com: "Sum a Union Query"
- Next in thread: Newbie: "Re: Sum a Union Query"
- Reply: Newbie: "Re: Sum a Union Query"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|
|