Hello all, i have 2 sql tables, and they each contain a column with bigint values. What i want to do is add up the values from both table, and then display it as 1 number. Supposing i have the following
table name: DownloadTable
fileName |DownloadSize|
file1 | 45
file2 | 50
file3 | 20
--------------
second table
table name: VideoTable
fileName | VideoSize |
file1 | 40
file2 | 60
file3 | 20
------------
Now, i want this to output 120, wich is the sum of the sum of the values of table 1 and 2 I've already tried the following:
SELECT SUM(DownloadTable.DownloadSize) + SUM(VideoTable.VideoSize) FROM DownloadTable, VideoTable
However, when i ran this, it gave me a huge number, that was far from accurate from what it was suppose to output. Can anyone help me?
Thanks in advance
Regards,
What is the expected output?|||in my real applicaion, the expected value was 3280. However, i got 37624.|||Based on the values you provided how do you arrive at 3280? Is there any business-forumula?|||well thats the thing. I dont know what i need to do to arrive at the expected value. And when i try to retrieve the sum of the tables indiidually, it works, and i get the expected output. But when it comes time to add up the values of those 2 tables, i get into trouble.
And what is a business-formula?
Thanks for your replies
|||
hervens:
Hello all, i have 2 sql tables, and they each contain a column with bigint values. What i want to do is add up the values from both table, and then display it as 1 number. Supposing i have the following
table name: DownloadTable
fileName |DownloadSize|
file1 | 45
file2 | 50
file3 | 20
--------------
second table
table name: VideoTable
fileName | VideoSize |
file1 | 40
file2 | 60
file3 | 20
------------
Now, i want this to output 120, wich is the sum of the sum of the values of table 1 and 2 I've already tried the following:
SELECT SUM(DownloadTable.DownloadSize) + SUM(VideoTable.VideoSize) FROM DownloadTable, VideoTable
However, when i ran this, it gave me a huge number, that was far from accurate from what it was suppose to output. Can anyone help me?
Is that your exact SQL statement? You've not specified which columns to join on, so your result set contains a row for every possible combination of every column in each table.
Instead, try something like this:
DECLARE @.Total BIGINT SELECT @.Total = SUM(DownloadSize) FROM DownLoadTable
SELECT @.Total = @.Total + (SELECT SUM(VideoSize) FROM VideoTable)
Hello tmorton, thx for your reply. However, when i pressed "test query" button in the dialog box to try to run it, the following error message poped up in a message box.
"The variable name "@.Total" has already been declared. Variable names must be unique within a query batch or stored procedure"
I also tried modifying the variable name total, but got the same error.
Im really sorry about this, im not much of an sql programmer. Just read a tutorial or 2 about it. Oh, and i copied the code you provided exactly
|||Try This Out......select t = sum(DownloadSize) + (select sum(VideoSize) from VideoTable) from DownloadSize
I think this should work for u.|||Wow, thank a lot Dhaliwal. Everything is working perfectly right now.
No comments:
Post a Comment