Solved

ASP MSSQL - SUM() total of 2 columns from 2 tables where date =

Posted on 2007-11-20
1,088 Views
Hi, I am trying 2 sum the total value of 2 columns from seperate tables.

I can get the result if both tables have a value, but if my second table named savedorders has no value then i get no value at all returned.

I tried it with union as well, but could work out how to sum the 2 results at the end.

here is my code so far

SELECT (select SUM(grandtotal) AS Total  FROM   dbo.savedorders  WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y')  +  (SELECT SUM(grandtotal)
FROM dbo.orders
WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y')  as today

0
Question by:sparky74

LVL 31

Expert Comment

unsure i think

SELECT (select SUM(grandtotal) AS Total,
(SELECT SUM(grandtotal)
FROM dbo.orders
WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y')  as today)

FROM   dbo.savedorders  WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y')
0

LVL 23

Accepted Solution

Ashish Patel earned 500 total points
Try this
``````Select Sum(Total) From (

select SUM(grandtotal) AS Total  FROM   dbo.savedorders  WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y'

Union

SELECT SUM(grandtotal) As total

FROM dbo.orders

WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y' ) A
``````
0

LVL 24

Expert Comment

I think you would have to use a union and do a subquery,

select sum(Total) from
(SELECT (select SUM(grandtotal) AS Total  FROM   dbo.savedorders  WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y')
UNION SELECT SUM(grandtotal)
FROM dbo.orders
WHERE DATEDIFF (d, orderdate, getdate()) = 0 AND cardvaild ='y') as derivedtable
0

LVL 31

Expert Comment

whoops i knew i forgot something...... ignore my comments....
0

Author Closing Comment

thanks for the quick response
0

Author Comment

thanks all, asvforce solution was 1st to appear on my screen for some reason and worked just as I needed. It took me a couple of hours getting to where I was, I see now where I went wrong with the select union I was using earlier today.

thanks for all your help
0

Join & Write a Comment Already a member? Login.

Suggested Solutions

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includâ€¦
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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

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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!