Solved

Sql Query

Posted on 2012-04-02
5
234 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

920 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

16 Experts available now in Live!

Get 1:1 Help Now