Previous Article SQL Server - Change Table Owner Name |
SQL Server, get the SUM of all values in a table, and add it to another tables values |
Next Article SQL Server Insert into Table from another table in the same database |
Coding Article #: 78 - Published On: June 23, 2017 @ 10:34:09 AM - Last Updated on: June 23, 2017
This article has been Favorited 0 timesJoin today, and add this to your favorites.
Share With Friends (Updated 6-8-2010)
Supported Files
No Files for this Article.
No Files for this Article.
No Screenshot Available
Get the Sum of all values in a table. This is handy for calculating the total hits of a website.
1st: Our Table name is [Category], and [SubCategory], with the Column name of [Hits].
This column is where all the views are stored, from the visitors to the site.
And the same thing applies to the next statement as well
If you have multiple tables that also include page views in this as well, you can do the same thing with it, as in the previous.
What if you wanted to add these two values together, well. That is simple to do as well.
1st: Our Table name is [Category], and [SubCategory], with the Column name of [Hits].
This column is where all the views are stored, from the visitors to the site.
And the same thing applies to the next statement as well
select sum(Hits) from SubCategory
If you have multiple tables that also include page views in this as well, you can do the same thing with it, as in the previous.
select sum(Hits) from Categories
What if you wanted to add these two values together, well. That is simple to do as well.
Select (select sum(Hits) from SubCategory)+(select sum(Hits) from Categories) as Views
Explaination of Code UseLet's take a look at what we have just done here.
We took each of the select SUM statements, and wrapped them with parentheses, with a + between the two.
The + is used to combine the two or more statements together, to give you a total value of all the SUMs of the tables.
Now, before we can do anything with them, we have to run a Select call at the end of our statement, with what the name of our value is going to be.
In this case, we named it [Views].
Post to Facebook about: SQL Server, get the SUM of all values in a table, and add it to another tables values