Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql Query

Posted on 2012-04-02
5
Medium Priority
?
242 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

610 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