i have following table:
topic_id(unique) forum_id forum_views
1002 1885 5
1003 1893 2
1004 1885 3
1005 1892 6
How can i get output like this: (unique forum_id with total forum views from above table)
forum_id forum_views
1885 8
1893 2
1892 6
ANY HELP IS APPRECIATED.
This is what i am trying:
select distinct forum_id, sum(topic_views) from table_name
The SQL Server SUM() function is an aggregate function that calculates the sum of all or distinct values in an expression. In this syntax: ALL instructs the SUM() function to return the sum of all values including duplicates. ALL is used by default.
You can use the SUM() function in a SELECT with JOIN clause to calculate the sum of values in a table based on a condition specified by the values in another table.
SELECT count(*) AS anyName FROM information_schema. columns WHERE table_name =' yourTableName'; Applying the above syntax in the example table with the name 'NumberOfColumns'. mysql> SELECT count(*) AS NUMBEROFCOLUMNS FROM information_schema.
To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.
select forum_id,
sum(forum_views) as forum_views
from jforum_topics
group by forum_id
Use this query select forum_id, sum(distinct forum_views) from table group by forum_id;
In this query sum distinct will do the sum of distinct value and do the grouping by the desired column.
If you want sum of all the values irrespective of distinctness then simply use sum(forum_views)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With