Solved

Sql Query

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job Hung 17 37
How can I retrieve the column names from a T-SQL table? 2 16
Checking for column changes SQL 2014 4 24
Split string into 3 separate fields 5 22
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

726 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