Solved

Sql Query

Posted on 2012-04-02
5
235 Views
Last Modified: 2012-04-02
Hi all i have created the following query

SELECT     SUM(OutstandingValue) AS Sum_OutstandingValue
FROM         dbo.V_TotalBackLog
WHERE     (it_due <= '06-30-2012')

union all

SELECT     SUM(sa_trvalue) AS Sum_invoiceValue
FROM         dbo.V_InvoiceValue
WHERE     (sa_trdate >= '07-01-2011') AND (sa_trdate <= '06-30-2012')

i think i have used the wrong function basically what i want to do is to add the sum from the first query to the sum of the the secound query to give me a total figure.

i have used the union all which places both values on seperate rows i just want the one value of both added together.

john
0
Comment
Question by:pepps11976
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Cluskitt
ID: 37796893
SELECT Sum_OutstandingValue+Sum_invoiceValue
FROM (SELECT     SUM(OutstandingValue) AS Sum_OutstandingValue
FROM         dbo.V_TotalBackLog
WHERE     (it_due <= '06-30-2012')

union all

SELECT     SUM(sa_trvalue) AS Sum_invoiceValue
FROM         dbo.V_InvoiceValue
WHERE     (sa_trdate >= '07-01-2011') AND (sa_trdate <= '06-30-2012')) t
0
 

Author Comment

by:pepps11976
ID: 37796911
Hi

I get the follwoing error

Invalid column name 'Sum_invoicevalue' with the above

John
0
 
LVL 18

Accepted Solution

by:
Cluskitt earned 500 total points
ID: 37796938
Sorry, my bad. You have to name both the same, so it would have to be:
SELECT SUM(Sum_OutstandingValue)
FROM (SELECT     SUM(OutstandingValue) AS Sum_OutstandingValue
FROM         dbo.V_TotalBackLog
WHERE     (it_due <= '06-30-2012')

union all

SELECT     SUM(sa_trvalue) AS Sum_OutstandingValue
FROM         dbo.V_InvoiceValue
WHERE     (sa_trdate >= '07-01-2011') AND (sa_trdate <= '06-30-2012')) t
0
 

Author Comment

by:pepps11976
ID: 37796948
Brilliant Thankyou
0
 
LVL 10

Expert Comment

by:plummet
ID: 37796970
You could try it this way:


Declare @totalValue numeric

SELECT     @totalValue = SUM(OutstandingValue) AS Sum_OutstandingValue
FROM         dbo.V_TotalBackLog
WHERE     (it_due <= '06-30-2012')

SELECT     @totalValue= @totalValue + SUM(sa_trvalue) AS Sum_invoiceValue
FROM         dbo.V_InvoiceValue
WHERE     (sa_trdate >= '07-01-2011') AND (sa_trdate <= '06-30-2012')

select  @totalValue

Open in new window

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now