Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-20
6
Medium Priority
?
1,098 Views
Last Modified: 2010-08-05
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
Comment
Question by:sparky74
[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
6 Comments
 
LVL 31

Expert Comment

by:James Murrell
ID: 20319202
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

by:
Ashish Patel earned 2000 total points
ID: 20319204
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

Open in new window

0
 
LVL 24

Expert Comment

by:mankowitz
ID: 20319208
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Expert Comment

by:James Murrell
ID: 20319240
whoops i knew i forgot something...... ignore my comments....
0
 

Author Closing Comment

by:sparky74
ID: 31410100
thanks for the quick response
0
 

Author Comment

by:sparky74
ID: 20319253
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

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

721 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